1. 2015-10-20 - Test depths within Java EE; Tags: Test depths within Java EE

    Test depths within Java EE

    TDD and BDD is a crucial discipline to write good and robust applications. Testing within the Java EE may cover several test depths. This post is only a short overview of possible involved products.

    Testing - test depth Local Testing

    • In the local testing part or Java SE Testing, we test against a single class.
    • Therefore the test execution is fast.
    • Preferred tools are JUnit (or TestNG), Mocking frameworks like Mockito

    Multi-Unit Testing

    • This test stage test against a service or a dedicated environment like CDI provider or JPA DB
    • For CDI tests: CDI-Unit, Apache DeltaSpike
    • Tests in CDI involves correct setup and wiring
    • For JPA tests: Testing against in-memory databases
    • Tests in JPA involves proper setup and logical queries correctness
    • since tests are executed against an environment, they need more time to be executed

    Integration Testing

    • In this test stage, you may test the component, subsystem or the whole application
    • Arquilian allows in-container testing with JBoss and other Java EE vendors
    • Apache OpenEJB can be used to start test against the whole Java EE Stack
    • Since you are testing against a defined target environment, it takes more time to test.
  2. 2015-10-19 - Parameterized Tests with JUnit; Tags: Parameterized Tests with JUnit

    Parameterized Tests with JUnit

    JUnit 4 has introduced a new feature: Parameterized tests. Parameterized tests allow developer to run the same test over and over again using different values. There are five steps, to create parameterized tests.

    • Annotate test class with @RunWith(Parameterized.class)
    • Create a public static method annotated with @Parameters that returns a Collection of Objects (as Array) as test data set.
    • Create a public constructor that takes in what is equivalent to one “row” of test data.
    • Create an instance variable for each “column” of test data.
    • Create your tests case(s) using the instance variables as the source of the test data.

    The test case will be invoked once per each row of data. In this post, we have an implementation of a counter string generator.

    This parameterized JUnit test covers random chosen numbers.

    public class TestStringUtilTest {
        private static final Logger LOGGER = LoggerFactory.getLogger(TestStringUtilTest.class);
        private final int length;
        private final String expectedResult;
        public TestStringUtilTest(Integer length,
                String expectedResult) {
            this.length = length;
            this.expectedResult = expectedResult;
         * Input test data, numbers chosen by random.org
         * @return test data
        public static Collection counterStrings() {
            return Arrays.asList(new Object[][] {
                    { -5, "*3*5*" },
                    { 0, "" },
                    { 1, "*" },
                    { 35, "2*4*6*8*11*14*17*20*23*26*29*32*35*" },
                    { 10, "*3*5*7*10*" },
                    { 23, "2*4*6*8*11*14*17*20*23*" },
                    { 32, "2*4*6*8*11*14*17*20*23*26*29*32*" },
                    { 88, "*3*5*7*10*13*16*19*22*25*28*31*34*37*40*43*46*49*52*55*58*61*64*67*70*73*76*79*82*85*88*" },
                    { 1024, "*3*5*7*9*12*15*18*21*24*27*30*33*36*39*42*45*48*51*54*57*60*63*66*69*72*75*78*81*84*87*90*93*96*99*103*107*111*115*119*123*127*131*135*139*143*147*151*155*159*163*167*171*175*179*183*187*191*195*199*203*207*211*215*219*223*227*231*235*239*243*247*251*255*259*263*267*271*275*279*283*287*291*295*299*303*307*311*315*319*323*327*331*335*339*343*347*351*355*359*363*367*371*375*379*383*387*391*395*399*403*407*411*415*419*423*427*431*435*439*443*447*451*455*459*463*467*471*475*479*483*487*491*495*499*503*507*511*515*519*523*527*531*535*539*543*547*551*555*559*563*567*571*575*579*583*587*591*595*599*603*607*611*615*619*623*627*631*635*639*643*647*651*655*659*663*667*671*675*679*683*687*691*695*699*703*707*711*715*719*723*727*731*735*739*743*747*751*755*759*763*767*771*775*779*783*787*791*795*799*803*807*811*815*819*823*827*831*835*839*843*847*851*855*859*863*867*871*875*879*883*887*891*895*899*903*907*911*915*919*923*927*931*935*939*943*947*951*955*959*963*967*971*975*979*983*987*991*995*999*1004*1009*1014*1019*1024*" }
        public void testCreateCounterString() throws Exception {
            LOGGER.info("Start test for counter string length {}", this.length);
            String actual = TestStringUtil.createCounterString(this.length);
            LOGGER.info("Generated counter string: {}", actual);
            assertEquals(String.format("generated counter string for number %d is ok", this.length), this.expectedResult, actual);
  3. 2015-07-06 - Testing with Maven; Tags: Testing with Maven

    Testing with Maven

    The Maven target test executes all tests under src/test. This article assumes you are familiar with JUnit tests.

    Skip tests

    Maven comes with an option to skip the unit test, -Dmaven.test.skip=true. If you absolutely must, you can also use the maven.test.skip property to skip compiling the tests. maven.test.skip is honored by Surefire, Failsafe and the Compiler Plugin.

    mvn install -Dmaven.test.skip=true
    mvn package -Dmaven.test.skip=true

    You can also skip the tests via command line by executing the following command:

    mvn install -DskipTests

    Skip integration tests

    Since skipTests is also followed by the Surefire Plugin, this will have the effect of not running any tests. If, instead, you want to skip only the integration tests being run by the Failsafe Plugin, you would use the skipITs property

    mvn install -DskipITs

    Running a Single Test

    «During development, you may run a single test class repeatedly. To run this through Maven, set the test property to a specific test case.»http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html

    mvn -Dtest=TestCircle test

    The value for the test parameter is the name of the test class (without the extension; we’ll strip off the extension if you accidentally provide one).

    You may also use patterns to run a number of tests:

    mvn -Dtest=TestCi*le test

    And you may use multiple names/patterns, separated by commas:

    mvn -Dtest=TestSquare,TestCi*le test

    Running a Set of Methods in a Single Test Class

    As of Surefire 2.7.3, you can also run only a subset of the tests in a test class. NOTE : This feature is supported only for Junit 4.x and TestNG. You must use the following syntax:

    mvn -Dtest=TestCircle#mytest test

    You can use patterns too

    mvn -Dtest=TestCircle#test* test

    As of Surefire 2.12.1, you can select multiple methods

    mvn -Dtest=TestCircle#testOne+testTwo test