【问题标题】:How to disable flapdoodle embedded mongodb in certain tests如何在某些测试中禁用 flapdoodle 嵌入式 mongodb
【发布时间】:2018-10-02 07:51:02
【问题描述】:

我基于Spring Initializr(gradle 风格)创建了一个 Spring Boot 应用程序。

我也加了

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

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

@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:

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

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

我的问题是:我想为某些测试禁用嵌入式 Mongo。实现这一目标的最简单方法是什么?

【问题讨论】:

    标签: java mongodb spring-boot junit spring-data


    【解决方案1】:

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

    @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

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

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

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

    并注释您的测试:

    @RunWith(SpringRunner.class)
    @SpringBootTest
    @EnableEmbeddedMongo
    public class StartMongoTest {
        //...
    
        @Test
        public void test() {
        }
    }
    

    【讨论】:

    • 将测试相关数据添加到生产代码中有点有趣,同意打开它比在测试中关闭它更有意义
    • 使用这种方法,我发现@DataMongoTest 已经完成了@EnableEmbeddedMongo 的目标。这使得首先停用自动配置的想法很棒
    猜你喜欢
    • 2020-09-05
    • 2019-09-16
    • 2019-01-01
    • 1970-01-01
    • 2016-08-26
    • 2018-10-09
    • 2019-06-30
    • 1970-01-01
    • 2018-10-04
    相关资源
    最近更新 更多