且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Junit 5 找实际环境配置运行测试

更新时间:2023-11-20 10:20:28

首先查看您的代码,您将模拟注入到测试类而不是实际服务中.这是您的实际测试的外观还是您只是复制了它以便不共享您的实际代码?

Firstly looking at your code you are injecting the mocks in to the test class rather than the actual service. Is this how your actual test looks or have you just copied this in so as to not share your actual code?

@InjectMocks
private MyServiceTest myServiceTest;

其次,这是一个单元测试,因此请删除 @SpringBootTest 注释.它增加了不必要的开销,并且可能由于缺少对您实际上没有测试的代码的配置而导致错误,因为您已经模拟了存储库.

Secondly this is a unit test so remove the @SpringBootTest annotation. It is adding unnecessary overhead and is probably causing the error through lack of configuration for code you are not actually testing having mocked the repository anyway.