はい、たとえば、あなたが求めるものを行うには、いくつかの方法があります。
- (春ブーツ1.4以降)テストクラスに
@SpringBootTest
アノテーションを使用。
- テスト内からSpringブートアプリケーションをプログラムで起動します。
最初のものは私の好きなものですが、より簡単なものですが、ユニットテストのコンテキストでのみ動作します。ここに例があります。
Application
という名前のクラスがあり、RESTモジュールに@SpringBootApplication
というアノテーションがあるとします。あなたは自分の統合テストモジュール内でこのようなテストを定義することによって、エンドポイントをテストすることができます。
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, properties = {"my.overriden.property=true"})
public class RestEndpointTest
{
// ...
}
ようにすることで、アプリケーションのコンテキスト全体が開始されます。さらに、必要に応じてテストを構成し、一部のプロパティをオーバーライドすることもできます(my.overridden.property
参照)。
@Configuration
@ComponentScan(basePackageClasses = {BaseClass.class})
@EnableJpaRepositories
@EntityScan
@EnableAutoConfiguration
public class SupportConfiguration
{
@Bean
public ARequiredBean bean()
{
return new ARequiredBean();
}
// etc...
}
を、あなたが他のコンテキストで実行するのと同じようにそれを使用して:
代わりに、例えば、他のモジュールから必要なすべてのクラスを参照する、テストモジュール内で独自の構成を定義することができます。
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = SupportConfiguration.class)
public class CustomTest
{
// ...
}
他の方法は、プログラムによってこのようなもので、あなたのRESTアプリケーションのインスタンスを起動するために、次のようになります。
public static void main(String[] args) throws IOException
{
try (ConfigurableApplicationContext context = SpringApplication.run(Application.class, args))
{
log.info("Server Started. Press <Enter> to shutdown...");
context.registerShutdownHook();
BufferedReader inReader = new BufferedReader(new InputStreamReader(System.in));
inReader.readLine();
log.info("Closing application context...");
context.stop();
}
log.info("Context closed, shutting down. Bye.");
System.exit(0);
}