Taeho Jeong
Taeho Jeong

Reputation: 51

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.

Unfortunately, during the process I faced some problems... one of them is here. It's about mocking function, specifically feign client.

The most weird thing is, when I used spring boot 2.1.6, everything works fine and mocking works well. 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. 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. So I think that the problem is on the spring.

Is there anything I can do to solve this problem? thanks.

I have these codes :

Test.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

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

and Application.java has the following annotataions :

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
...

in short : same code, same process, different version, error.

Upvotes: 2

Views: 3392

Answers (1)

Taeho Jeong
Taeho Jeong

Reputation: 51

I found an answer. The version compatibility between spring boot and spring cloud didn't match. When I use 2.2.5.RELEASE for spring boot, the error disappears.

Upvotes: 2

Related Questions