【发布时间】: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