如何在某些测试中禁用flappedoodle嵌入式mongodb [英] How to disable flapdoodle embedded mongodb in certain tests

查看:123
本文介绍了如何在某些测试中禁用flappedoodle嵌入式mongodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基于 Spring Initializr (渐变风味)创建了一个Spring Boot应用程序.

I created a spring boot application based on Spring Initializr (gradle flavour).

我还添加了

compile('org.springframework.boot:spring-boot-starter-data-mongodb')

使用MongoDB进行持久化.我还添加了一个运行良好的简单集成测试:

To use a MongoDB for persistence. I also added a simple integration test that works fine:

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class TileServiceApplicationTests {

    @Autowired
    private MockMvc mvc;

    @Autowired
    private UserSettingRepository userSettingRepository;

    @Test
    public void contextLoads() throws Exception {
        Folder folder = random( Folder.class, "color", "elements" );
        EserviceTile eserviceTile1 = random( EserviceTile.class , "color");
        EserviceTile eserviceTile2 = random( EserviceTile.class, "color" );
        folder.setElements( Arrays.asList(eserviceTile1) );
        TileList usersTiles = new TileList( Arrays.asList( folder, eserviceTile2 ) );

        userSettingRepository.save( new UserSetting( "user1", usersTiles ));


        String string = mvc.perform( get( "/user1" ) ).andExpect( status().isOk() ).andReturn().getResponse().getContentAsString();
        Assert.assertThat(string, allOf( containsString( eserviceTile1.getName() ), containsString( eserviceTile2.getName() ) ) );
    }

}

如果mongodb在默认端口上运行,我看到数据仍然存在.成为独立于我刚刚添加的运行mongo:

If a mongodb is running on default port i see the data persisted. To be independent of running mongo i just added:

 testCompile('de.flapdoodle.embed:de.flapdoodle.embed.mongo:2.1.1')

瞧瞧,测试在没有mongo的情况下进行!(没什么可补充的)

and voila the test runs without mongo! (nothing else to add)

我的问题是:我想为某些测试禁用嵌入式Mongo .最简单的方法是什么?

My problem is: I want to disable the embedded Mongo for certain tests. What is the easiest way to achieve that?

推荐答案

Embedded Mongo守护程序从 EmbeddedMongoAutoConfiguration 启动.您可以通过从扫描中排除 EmbeddedMongoAutoConfiguration 来禁用单个测试中的守护程序启动:

Embedded Mongo daemon is started with EmbeddedMongoAutoConfiguration. You can disable daemon start in a single test by excluding EmbeddedMongoAutoConfiguration from scan:

@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource(properties = "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration")
public class DoNotStartMongoTest {
    //...

    @Test
    public void test() {
    }
}

我希望使用相反的功能:按需启动嵌入式Mongo守护程序.为此,您需要在生产代码中排除 EmbeddedMongoAutoConfiguration :

I would prefer an opposite functionality: start embedded Mongo daemon on demand. To do this you need to exclude EmbeddedMongoAutoConfiguration in production code:

@EnableMongoRepositories
@SpringBootApplication(exclude = EmbeddedMongoAutoConfiguration.class)
public class MySpringBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(MySpringBootApplication.class, args);
    }
}

然后在测试代码中添加注释,该注释将启用嵌入式Mongo守护程序启动:

Then in test code add annotation which will enable embedded Mongo daemon start:

@Retention(RUNTIME)
@Target(TYPE)
@Import(EmbeddedMongoAutoConfiguration.class)
public @interface EnableEmbeddedMongo {
}

并注释您的测试:

@RunWith(SpringRunner.class)
@SpringBootTest
@EnableEmbeddedMongo
public class StartMongoTest {
    //...

    @Test
    public void test() {
    }
}

这篇关于如何在某些测试中禁用flappedoodle嵌入式mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆