I'm trying to write a test cases for createEmployee and updateEmployee by mocking the repository class. But, it's failing with below error.
Note : Other methods for find, findAll are working fine.
java.lang.NullPointerException: Cannot invoke "reactor.core.publisher.Mono.thenReturn(Object)" because the return value of "com.springflexcounchdb.dao.IEmployeeDAO.create(com.springflexcounchdb.model.Employee)" is null
at com.springflexcounchdb.test.service.EmployeeServiceTest.createEmployeeTest(EmployeeServiceTest.java:71)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:93)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
//test class where repository class is mocked and update service method called
@RunWith(SpringRunner.class)
@WebFluxTest(EmployeeService.class)
public class EmployeeServiceTest {
@Autowired
private EmployeeService employeeService;
@MockBean
@Qualifier("employeeDAO")
private IEmployeeDAO employeeDAO;
@Test
public void createEmployeeTest() {
List<AddressDTO> addressDTOList= new ArrayList<>();
addressDTOList.add(new AddressDTO("address"));
List<Address> addressList= new ArrayList<>();
addressList.add(new Address("address"));
EmployeeDTO employeeDTO= new EmployeeDTO("1", "1", "1-1234", "Test-Employee", 20, addressDTOList);
Employee employee = new Employee("1", "1", "1-1234", "Test-Employee", 20, addressList);
Mono<Employee> monoEmployee = Mono.just(employee);
when(employeeDAO.create(employee).thenReturn(Mono.just("1")));
Mono<String> responseEmployee = employeeService.create(employeeDTO);
try {
assertEquals(true, responseEmployee.toFuture().get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
//service class where update method is present
@Service
public class EmployeeService implements IEmployeeService {
@Autowired
@Qualifier("employeeDAO")
IEmployeeDAO iEmployeeDAO;
public Mono<EmployeeDTO> update(EmployeeDTO e) {
return MappingDtoToEntity.convertEmpoyeeToMono(iEmployeeDAO.update(MappingDtoToEntity.convertEmployeeEntity(e)));
}
}