简体   繁体   English

如何在某些测试中禁用flappedoodle嵌入式mongodb

[英]How to disable flapdoodle embedded mongodb in certain tests

I created a spring boot application based on Spring Initializr (gradle flavour). 我基于Spring Initializr (渐变风味)创建了一个Spring Boot应用程序。

I also added 我还加了

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

To use a MongoDB for persistence. 使用MongoDB进行持久化。 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() ) ) );
    }

}

If a mongodb is running on default port i see the data persisted. 如果mongodb在默认端口上运行,我看到数据仍然存在。 To be independent of running mongo i just added: 为了独立于运行mongo,我刚刚添加了:

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

and voila the test runs without mongo! 瞧,测试无需mongo! (nothing else to add) (没什么可补充的)

My problem is: I want to disable the embedded Mongo for certain tests . 我的问题是: 我想对某些测试禁用嵌入式Mongo What is the easiest way to achieve that? 最简单的方法是什么?

Embedded Mongo daemon is started with EmbeddedMongoAutoConfiguration . 嵌入式Mongo守护程序从EmbeddedMongoAutoConfiguration启动。 You can disable daemon start in a single test by excluding EmbeddedMongoAutoConfiguration from scan: 您可以通过从扫描中排除EmbeddedMongoAutoConfiguration来禁用单个测试中的守护程序启动:

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

    @Test
    public void test() {
    }
}

I would prefer an opposite functionality: start embedded Mongo daemon on demand. 我更喜欢相反的功能:按需启动嵌入式Mongo守护程序。 To do this you need to exclude EmbeddedMongoAutoConfiguration in production code: 为此,您需要在生产代码中排除EmbeddedMongoAutoConfiguration

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

Then in test code add annotation which will enable embedded Mongo daemon start: 然后在测试代码中添加注释,这将使嵌入式Mongo守护程序启动:

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

And annotate your test: 并注释您的测试:

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

    @Test
    public void test() {
    }
}

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

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