简体   繁体   English

注释@MockBean 在 spring 启动 2.3 上不起作用(未注入?)

[英]annotation @MockBean not working(not injected?) on spring boot 2.3

I'm upgrading spring boot version from 2.1.6.RELEASE to 2.3.0.RELEASE due to some reasons.由于某些原因,我正在将 spring 引导版本从 2.1.6.RELEASE 升级到 2.3.0.RELEASE。

Unfortunately, during the process I faced some problems... one of them is here.不幸的是,在此过程中我遇到了一些问题……其中一个就在这里。 It's about mocking function, specifically feign client.是关于mocking function,具体feign客户端。

The most weird thing is, when I used spring boot 2.1.6, everything works fine and mocking works well.最奇怪的是,当我使用 spring 启动 2.1.6 时,一切正常,mocking 运行良好。 But after upgraded the version, I got an error:但是升级版本后,我得到了一个错误:

java.lang.RuntimeException: com.netflix.client.ClientException: Load balancer does not have available server for client

which means the mocking process didn't work since the feign client actually tried to find a server.这意味着 mocking 进程不起作用,因为 feign 客户端实际上试图找到一个服务器。 Is there any changes to apply I've omitted?是否有任何更改要应用我省略了?

Just to be sure, since I found that spring-boot-starter-test module upgraded the dependency mockito version from 2.24.2(not sure) to 3.3.2(also not sure), I tried to downgrade mockito version to the origin and it shows nothing changed.可以肯定的是,由于我发现 spring-boot-starter-test 模块将依赖项 mockito 版本从 2.24.2(不确定)升级到 3.3.2(也不确定),所以我尝试将 mockito 版本降级到原点和它显示没有任何变化。 So I think that the problem is on the spring.所以我认为问题出在spring上。

Is there anything I can do to solve this problem?我能做些什么来解决这个问题吗? thanks.谢谢。

I have these codes:我有这些代码:

Test.java测试.java

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Application.class)
@AutoConfigureMockMvc
public class Test{
    @MockBean private FeignProxy feignProxy;
    @Autowired private ApplicationService applicationService;

    @Test
    @Transactional
    public void doTest(){
        when(feignProxy.doProxy()).thenReturn(response);
        assertDoesNotThrow(() -> {
            applicationService.doService(); // exception occurs
        }
    }

ApplicationServiceImpl.java ApplicationServiceImpl.java

@Component
public class ApplicationServiceImpl implements ApplicationService{
    @Autowired private FeignProxy feignProxy;
    public void doService(){
        ...
        feignProxy.doProxy();
        ...
    }
}

and Application.java has the following annotataions:和Application.java有如下注解:

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
...

in short: same code, same process, different version, error.简而言之:相同的代码,相同的流程,不同的版本,错误。

I found an answer.我找到了答案。 The version compatibility between spring boot and spring cloud didn't match. spring 启动和 spring 云之间的版本兼容性不匹配。 When I use 2.2.5.RELEASE for spring boot, the error disappears.当我使用 2.2.5.RELEASE 进行 spring 启动时,错误消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM