At work I was writing test cases for methods for a dao class. Suddenly I realized that the test methods do not work the in the sequence that I wrote them, eventually one test case has failed because deletion method is being conducted before the creation method. Then I’ve checked it online and after JUnit 4.11 there is a new annotation called @FixMethodOrder and this annotation takes a parameter from such list and I’ve implemented MethodSorters.NAME_ASCENDING to have the method be conducted by name.
So by now my problem was not solved, and yet I’ve had to alter my methods names with a such prefix stage1_ETC, stage2_ETC so this way I name the one which I want to be conducted in the name order. The below I’m sharing a sample code snipped for you to study
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:*applicationContext-test.xml") @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class BusinessObjectsDAOTest { final String testUserName = "TESTTESTTEST"; final String testUserNameLastName = "TEST"; final String testUserEmail = "TEST@TESTDOMAIN.COM"; final String authGroup1 = "GRP_PAL_VIEW_ON_DEMAND"; final String authGroup2 = "GRP_PAL_VIEW_ON_DEMAND_SAVE_AS"; final String authGroup3 = "GRP_PAL_POWER_USER"; final String authGroup4 = "GRP_PAL_POWER_USER_SAVE_AS"; @Autowired BusinessObjectsService boService; @Before public void setUp() throws Exception { boService.beginEnterpriseSession("USERNAME", "PASSWORD", "SERVERADDR"); } @After public void tearDown() throws Exception { boService.terminateEnterpriseSession(); } @Test public void stage01_CheckUserExistenceTrueCase() { assertEquals(true, boService.checkUserExistence("90003327")); } @Test public void stage02_CheckUserExistenceFalseCase() { assertEquals(false, boService.checkUserExistence("THISUSERDOESNOTEXIST")); } @Test public void stage03_CollectAuthorizationGroupsAmountTest() { List<String> list = boService.collectAuthorizationGroups(); assertEquals(4, list.size()); } @Test public void stage04_CollectAuthorizationGroupsDataTest() { List<String> list = boService.collectAuthorizationGroups(); List<String> manualList = new ArrayList<String>(); manualList.add(authGroup1); manualList.add(authGroup2); manualList.add(authGroup3); manualList.add(authGroup4); Collections.sort(list); Collections.sort(manualList); assertEquals(manualList, list); } @Test public void stage05_CreateUser(){ boService.createUser(testUserName, testUserNameLastName, testUserNameLastName, testUserEmail); boolean userExistence = boService.checkUserExistence(testUserName); assertEquals(true, userExistence); } @Test public void stage06_getUserInfo(){ String[] userData = boService.getUserInfo(testUserName); String[] userDataManual = {testUserNameLastName + " " + testUserNameLastName, testUserEmail}; assertArrayEquals(userDataManual, userData); } @Test public void stage07_JoinUserToAuthGroupToGRP_PAL_VIEW_ON_DEMAND(){ boService.joinUserToAuthGroup(testUserName, "GRP_PAL_VIEW_ON_DEMAND"); assertTrue(boService.acquireUserAuthGroup(testUserName).equals(authGroup1)); } @Test public void stage08_RemoveUserFromAuthGroupToGRP_PAL_VIEW_ON_DEMAND(){ boService.removeUserFromGroup(testUserName, "GRP_PAL_VIEW_ON_DEMAND"); assertFalse(boService.isUserInGroup(testUserName, authGroup1)); } @Test public void stage09_JoinUserToAuthGroupToGRP_PAL_VIEW_ON_DEMAND_SAVE_AS(){ boService.joinUserToAuthGroup(testUserName, "GRP_PAL_VIEW_ON_DEMAND_SAVE_AS"); assertTrue(boService.acquireUserAuthGroup(testUserName).equals(authGroup2)); } @Test public void stage10_RemoveUserFromAuthGroupToGRP_PAL_VIEW_ON_DEMAND_SAVE_AS(){ boService.removeUserFromGroup(testUserName, "GRP_PAL_VIEW_ON_DEMAND_SAVE_AS"); assertFalse(boService.isUserInGroup(testUserName, authGroup2)); } @Test public void stage11_DeleteUser(){ boService.deleteUser(testUserName); boolean userExistence = boService.checkUserExistence(testUserName); assertEquals(false, userExistence); } }
Output