Search code examples
junitjunit4

How to know current @Test method in @before method in Junit 4


I have following code in JUnit test class. How to know in @Before method, which @Test method is to be executed now. Since @Before method is getting called prior to both @Test method. I want to write code in @Before such that it will setup relevant test data for corresponding @Test Method.

/*
         * @Before is called before executing each test method.
         * We are using it to setup test data before executing test case
         * */
        @Before
        public void prepareForTest()throws Exception{
            System.out.println("prepareForTest called");

        }
        
        /**
         * This method to check sub requirements are present 
         * 1. AssertNotNull variant
         * 2. Condition that MapList is not empty 
         */
        @Test
        public void testGetSubRequirementNotNull()throws MatrixException{
            System.out.println("testGetSubRequirementNotNull called");
            
            String strReqObjectId = propertySet.getProperty("Requirement.testGetSubRequirementNotNull.objectId");
            
            RequirementService reqService = serviceFactory.getRequirementService(context);
            MapList mlSubReqList = reqService.getSubRequirement(context, strReqObjectId);
            
            assertNotNull("SubRequirement List is null", mlSubReqList);
            assertFalse("SubRequirement list is empty", mlSubReqList.isEmpty());
            
        }
    
        /**
         * This method is to check sub requirements are not present
         * 1. AssertNull variant
         * 2. Condition that MapList is empty 
         */
        @Test
        public void testGetSubRequirementNull()throws MatrixException{
            System.out.println("testGetSubRequirementNull called");
            
            String strReqObjectId = propertySet.getProperty("Requirement.testGetSubRequirementNotNull.objectId");
            
            RequirementService reqService = serviceFactory.getRequirementService(context);
            MapList mlSubReqList = reqService.getSubRequirement(context, strReqObjectId);
            
            assertNull("SubRequirement List is not null", mlSubReqList);
            assertTrue("SubRequirement list is not empty", mlSubReqList.isEmpty());
            
        }

Solution

  • The documentation of @Before says:

    When writing tests, it is common to find that several tests need similar objects created before they can run.

    In my experience you should only create Objects in your @Before Methode that are used by all of the testmethods in your class.

    If one of your tests does need an object that is only used there the test should instantiate this object by itself.

    If a few (not all) of your tests needs the same (or similar) object I would recommend to extract the instantiation of the object in a private helper method of the class that is called by each testmethod. This would also adhere to the DRY principle.

    tl;dr;
    I don't think you need to know which test will be executed next. Each Test should instantiate the objects it needs itself.