vendredi 17 avril 2020

how to use junit and mockito when we have a mulit layer service call approach in a restfull web service

i am using spring tool suite to write a code .there are 4 layers restContoller,buisnesslogic,domain ,service.... i want to test for a method of business logic layer where it calls a method of dao which finally calls a method of service layer to return a simple primitive value... to make it clear in the businesslogic class i have autowired domain class ,and in the domain class i have autowired the service classs..the problem that i am facing iss when i run the test class i am getting NullPointerException i am attaching the code for the test class... kindly help if possible

@ExtendWith(MockitoExtension.class)
class CustomerBlTest {
    @Mock
    CustomerService mockService;
    @Autowired
    CustomerDO customerDo;

    @Autowired
    @InjectMocks
    CustomerBl bl;       //buisnesslogic class
    @Test
    void checkForGetInteger() {
        when(mockService.getIntegerFfromService()).thenReturn(3);

        int actual = bl.getInteger();
        Assertions.assertEquals(3, actual);
    }
}

Aucun commentaire:

Enregistrer un commentaire