Skip to content

ksevindik/spring-core-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Core Examples

Customizing ApplicationContext during SpringBoot Integration Tests

Usage of Configuration

It is possible to define an inner class annotated with @Configuration within the integration test class, however, from now on, the application context bootstrapped when we run tests inside the test class will only include the beans that defined within this configuration class. Even context component scan won't work unless it is enabled by that configuration class, therefore, none of those beans defined via stereotype annotations won't be available either.

@SpringBootTest
class BarTest {
@Configuration
class Config {
}

@SpringBootTest
class FooTest {
@Configuration
@Import(BazConfig::class)
class Config {
}

Usage of TestConfiguration

The normal way to override any beans specific to the integration test class is to create an inner configuration class with @TestConfiguration annotation, which will have those bean definition overrides, or any specific configurations, special to that test class. Defining such a configuration class won't block normal application context configuration, so any configuration classes and components will be automatically discovered and loaded when the test is run.

@TestConfiguration
class FooConfig {
@Bean
fun foo() : Foo {
return Foo()
}

If your test configuration class annotated with @TestConfiguration is defined as a top level class, then it won't be included in the application context automatically. Instead, it must be added explicitly either via @Import on top of a configuration class you just added specific for this test class, or via @ContextConfiguration annotation as a bean class.

@TestConfiguration
class BarConfig {
@Bean
fun bar() : Bar {
return Bar()
}
}

@SpringBootTest
class Bar3Test {
@TestConfiguration
@Import(BarConfig::class, Bat::class)
class Config {

@SpringBootTest
@ContextConfiguration(classes = [BarConfig::class, Bat::class])
class Bar2Test {
@Autowired

Usage of Configuration & TestConfiguration in Base Test Classes

If the configuration class with @Configuration annotation is defined within the BaseTest class, it will be discovered when test case within the SubTest class is run, but if there is an overriden bean definition within that configuration class it won't take effect.

abstract class BaseTest {
@TestConfiguration
class BaseTestConfig {
@Bean
fun message() : String = "base test message"
@Bean
fun message2() : String = "message2"
}
}
class SubTest : BaseTest() {
@Autowired
private lateinit var applicationContext: ApplicationContext
@Autowired(required = true)
private lateinit var bak: Bak
@Test
fun `message should not be overriden`() {

For the bean definition override to work, you must create a config class within the SubTest, and annotate both config classes either with @Configuration or @TestConfiguration. However, be aware of that if you annotate them with @Configuration, then the above limitation will be in act. Therefore, it is better to go with the @TestConfiguration. You need to import the config class within BaseTest if both SubTest and BaseTest config classes are marked with @TestConfiguration.

abstract class Base2Test {
@TestConfiguration
class Base2TestConfig {
@Bean
fun message() : String = "base 2 test message"
@Bean
fun message2() : String = "message2"
}
}
class Sub2Test : BaseTest() {
@Autowired
private lateinit var applicationContext: ApplicationContext
@Autowired(required = true)
private lateinit var bak: Bak

Usage of TestComponent

@TestComponent annotation is a stereotype annotation which can be used to define beans that are only aimed for testing purposes. Therefore, putting that annotation on top the class alone wouldn't work. Instead, you need to explicitly tell Spring Test infra to load that bean class when the test is run.

One way to load is make use of @Import on top of the inner configuration class in order to specify that bean class should be explicitly listed to be loaded during test run. The other way is to specify that bean class via @ContextConfiguration annotation on top of the test class.

@SpringBootTest
class Bar3Test {
@TestConfiguration
@Import(BarConfig::class, Bat::class)
class Config {

@SpringBootTest
@ContextConfiguration(classes = [BarConfig::class, Bat::class])
class Bar2Test {
@Autowired

The whole benefit of using either @TestComponent or @TestConfiguration outside a test class is to make those test components and configurations available for other test classes as well. Otherwise, you just only make use of @TestConfiguration annotation within an inner configuration class.

Usage of Import

The role of @Import annotation is to import any Spring ApplicationContext configuration class or component class into the ApplicationContext of the test being run.

Customization of Liveness and Readiness Probes

By default, liveness and readiness probes only include livenessState and readinessState components which doesn't do anything useful. It might be useful to add other components into those probes so that status of the application should be queried better. You can achieve this by adding following properties into the application.properties file.

https://github.com/ksevindik/spring-core-examples/blob/f6b6b52f93d9e0a89bcb2f0e670f6604d7d80c5d/src/main/resources/application.properties#L7-L9

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages