どのように特定のテストで無効flapdoodle組み込みのMongoDBへ

dermoritz:

私は春に基づくブートアプリケーションを作成した春Initializr(Gradleの味)。

私も追加します

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のデフォルトのポートで実行されている場合、私は、データが持続ご覧ください。私はちょうど追加モンゴを実行しているとは独立しています:

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

出来上がりテストはモンゴなしで実行されます!(何も追加します)

私の問題は次のとおりです。私は特定のテストのために埋め込まれたモンゴを無効にしたいですそれを達成するための最も簡単な方法は何ですか?

ヴィタリー:

組み込み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() {
    }
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=222451&siteId=1