简体   繁体   English

如何在Spring Boot应用程序中模拟外部依赖关系?

[英]How do you mock external dependencies in spring boot application?

I have a requirement to test a SpringBoot application where I run the tests against the end point (for now, locally). 我需要测试一个SpringBoot应用程序,在该应用程序中针对终点(目前是本地)运行测试。

There is one call from a service to external service ( s3 ), and I just need to mock this, so that we don't do a live call to s3 from our test. 从服务到外部服务( s3 )有一个调用,我只需要对此进行模拟,这样我们就不会从测试中对s3进行实时调用。

I use Mockito for mocking. 我使用Mockito进行嘲笑。

The call stack: 调用堆栈:

Controller -service

                   -external service.

From my test, I just hit the end point url ( localhost:8080/actions/domyjob ) 从我的测试中,我只是点击了终点网址( localhost:8080/actions/domyjob

This is my controller: 这是我的控制器:

@RestController
@RequestMapping("/myjob")
public class MyController{

    @Autowired
    private MyService myService;

    @RequestMapping(path = "/doJobInMyService", method = POST)
    public void doJobInMyService(){
        myService.doMyJob()
    }

}

This is my service: 这是我的服务:

@Service
public class MyService {

    @Autowired
    private s3Client AmazonS3Client;

     doMyJob() {
    s3Client.putObject(new PutObjectRequest());
}
}

If you see, if I want to test the entire flow, by calling localhost:8080/myjob/doJobInMyService and just mock s3Client.putObject(new PutObjectRequest()) , so that external calls to s3 is not done. 如果您看到了,如果我想测试整个流程,请通过调用localhost:8080/myjob/doJobInMyService并仅模拟s3Client.putObject(new PutObjectRequest()) ,这样就不会对s3进行外部调用。

Tried this, but I had still no luck: 试过这个,但我还是没有运气:

@ActiveProfiles("MyTestConfig")
@RunWith(SpringJUnit4ClassRunner.class)
public class MyTest extends BaseTest {
    @Autowired
    private AmazonS3Client amazonS3Client;

    @Test
    public void testMyResponse() {
        try {
            Mockito.when(amazonS3Client.putObject(anyObject())).thenReturn(new PutObjectResult());
            assertNotNull(getMyClient().doMyJob());
        } catch(Exception e) {

        }
    }
}

@Profile("MyTestConfig")
@Configuration
public class MyTestConfiguration {

    @Bean
    @Primary
    public AmazonS3Client amazonS3Client() {
        return Mockito.mock(AmazonS3Client.class);
    }

Since Spring Boot 1.4.x, there is Mockito mocking of Spring beans natively supported via annotation @MockBean . 从Spring Boot 1.4.x开始,通过注释@MockBean本地支持Spring Bean的@MockBean See this section of Spring Boot docs for more info. 有关更多信息,请参见Spring Boot文档的本部分

I created blog post on the topic . 我创建了有关该主题的博客文章 It contains also link to Github repository with working example. 它还包含带有示例的Github存储库的链接。

The trick is using test configuration, where you override original spring bean (eg s3Client in your case) with fake one. 诀窍是使用测试配置,在该配置中,您使用伪造的Spring Bean覆盖原始的Spring Bean(例如您的情况下为s3Client )。 You can use @Primary and @Profile annotations for this trick. 您可以将@Primary@Profile批注用于此技巧。

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

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