Springブートテストで自動構成クラスを除外する
1. 概要
このクイックチュートリアルでは、自動構成クラスをSpring Bootテストから除外する方法について説明します。
Spring Bootの自動構成機能は、多くのセットアップを処理してくれるので非常に便利です。 ただし、特定の自動構成がモジュールのテストに干渉することを望まない場合、これはテスト中にも問題になる可能性があります。
この一般的な例は、セキュリティの自動構成です。これは、例でも使用します。
2. テスト例
まず、テスト例を見てみましょう。
シンプルなホームページを備えた安全なSpring Bootアプリケーションを用意します。
認証なしでホームページにアクセスしようとすると、「401UNAUTHORIZED」と応答します。
REST-assuredを使用して電話をかけるテストでこれを見てみましょう。
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
public class AutoConfigIntegrationTest {
@Test
public void givenNoAuthentication_whenAccessHome_thenUnauthorized() {
int statusCode = RestAssured.get("http://localhost:8080/").statusCode();
assertEquals(HttpStatus.UNAUTHORIZED.value(), statusCode);
}
}
一方、認証を使用してホームページに正常にアクセスできます。
@Test
public void givenAuthentication_whenAccessHome_thenOK() {
int statusCode = RestAssured.given().auth().basic("john", "123")
.get("http://localhost:8080/")
.statusCode();
assertEquals(HttpStatus.OK.value(), statusCode);
}
次のセクションでは、テストの構成からSecurityAutoConfigurationクラスを除外するさまざまな方法を試します。
3. @EnableAutoConfigurationを使用する
テストの構成から特定の自動構成クラスを除外する方法は複数あります。
まず、 @EnableAutoConfiguration(exclude = {CLASS_NAME})アノテーションを使用する方法を見てみましょう。
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
@EnableAutoConfiguration(exclude=SecurityAutoConfiguration.class)
public class ExcludeAutoConfigIntegrationTest {
@Test
public void givenSecurityConfigExcluded_whenAccessHome_thenNoAuthenticationRequired() {
int statusCode = RestAssured.get("http://localhost:8080/").statusCode();
assertEquals(HttpStatus.OK.value(), statusCode);
}
}
この例では、exclude属性を使用してSecurityAutoConfigurationクラスを除外しましたが、自動構成クラスのいずれでも同じことができます。
これで、認証なしでホームページにアクセスするテストを実行でき、失敗することはなくなりました。
4. @TestPropertySourceを使用する
次に、 @TestPropertySourceを使用して、プロパティ「spring.autoconfigure.exclude」を挿入できます。
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
@TestPropertySource(properties =
"spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration")
public class ExcludeAutoConfigIntegrationTest {
// ...
}
プロパティの完全なクラス名(パッケージ名+単純な名前)を指定する必要があることに注意してください。
5. プロファイルの使用
プロファイルを使用して、テスト用にプロパティ「spring.autoconfigure.exclude」を設定することもできます。
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
@ActiveProfiles("test")
public class ExcludeAutoConfigIntegrationTest {
// ...
}
そして、すべての「test」プロファイル固有のプロパティをapplication-test.propertiesに含めます。
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
6. カスタムテスト構成の使用
最後に、テスト用に別の構成アプリケーションを使用できます:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class, webEnvironment = WebEnvironment.DEFINED_PORT)
public class ExcludeAutoConfigIntegrationTest {
// ...
}
そして、自動構成クラスを @SpringBootApplication(exclude = {CLASS_NAME})から除外します。
@SpringBootApplication(exclude=SecurityAutoConfiguration.class)
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
7. 結論
この記事では、SpringBootテストから自動構成クラスを除外するさまざまな方法について説明しました。
完全なソースコードは、GitHubでから入手できます。