无法调用 "reactor.core.publisher.Mono.thenReturn(Object)",因为 return 值为空

Cannot invoke "reactor.core.publisher.Mono.thenReturn(Object)" because the return value is null

我正在尝试通过模拟存储库 class 为 createEmployee 和 updateEmployee 编写测试用例。但是,它因以下错误而失败。

注意:查找、findAll 的其他方法工作正常。

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.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.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.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)

//测试class,其中模拟存储库class并调用更新服务方法

    @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();
            }                   
        }
    }

//存在更新方法的服务class

    @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)));
       }
    }

您的代码 ) 错位了

when(employeeDAO.create(employee).thenReturn(Mono.just("1")))

应该是

when(employeeDAO.create(employee)).thenReturn(Mono.just("1"))

使用您的代码,它认为您试图模拟 Mono class 上的 thenReturn 方法调用,这是错误的。