I am using the JUnit for code coverage in my project. For db i am using the dbunit as like mock DB. When i am running JUnit from Eclipse UI its getting passed, but its getting failed when run through maven.
Above set up is running fine in JDK 1.6.25 by using maven and its started failing when upgraded to 1.8.51. I had updated the maven compiler plugin, its doesn't work. I am used below versions, junit - 4.7 2.dbunit - 2.4.8 hsqldb - 2.0.0 maven - 2.2.1.
Issue:
-> All test cases which ran fine in JAVA 1.6, started failing on migrating to JDK 1.8.51. -> Due this we faced build failure issue and also code coverage reduction.
Root Cause:
-> JUnit uses Java reflection to get the test methods from Test classes. In JAVA 1.6 test method order returned as same as declaration in source file. -> But from JAVA 7 onwards the methods order returned the by JVM is not same as the source file, it will be returned randomly. -> Since our test cases are dependent on each other, due to order change it started failing. For Example below test cases are using the same data (Mock DB) for execution, -> AddOperationTestCase() -> EditOperationTestCase() -> DeleteOperationTestCase() If delete run first due JVM random order, for Add and Edit data won't be available it will fail.
Solution :
-> I had tried to find options in JUnit and Sure Fire plugin to maintain same order as like source file, but I could not find feasibility there. -> I have identified the class which will returns the order of execution in JUnit library and override that accordingly to run it source file order. -> As of now I had added this annotation wrapper to failed classes, now build is running successfully.
Link for Wrapper class: https://somethingididnotknow.wordpress.com/2014/03/07/run-junit-tests-in-order/