gpt4 book ai didi

junit - 无法调用 "reactor.core.publisher.Mono.thenReturn(Object)",因为返回值为空

转载 作者:行者123 更新时间:2023-12-02 18:22:37 30 4
gpt4 key购买 nike

我正在尝试通过模拟存储库类来为 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$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)

//模拟存储库类并调用更新服务方法的测试类

    @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
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 类上的 thenReturn 方法调用,这是错误的。

关于junit - 无法调用 "reactor.core.publisher.Mono.thenReturn(Object)",因为返回值为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70669034/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com