0

I want to run my Spring Boot application in the dev mode:

My main (prod app):

fun main(args: Array<String>) {
    runApplication<MyApp>(args = args, init = {
        addInitializers(BeansInitializer())
    })
}

App in the dev mode:

import com.myApp.main as prodMain

object MyApplicationTest {
    @JvmStatic
    fun main(args: Array<String>) {
        SpringApplication.from(::prodMain)
            .with(RedisContainerDevMode::class.java)
            .run(*args)
    }
}
@TestConfiguration
class RedisContainerDevMode {

    @Bean
    @ServiceConnection("redis")
    fun redis(): GenericContainer<*> =GenericContainer("redis:latest").withExposedPorts(6379)

}

Apart from this, I have a context initializer that is needed for tests using @SpringBootTest

context:
  initializer:
    classes: com.myApp.BeansInitializer

This initializer causes Bean definitions to be loaded twice and crashes the development app.

How I can simultaneously have:

  1. Run tests using @SpringBootTest and have beans initialized from BeansInitializer
  2. Be able to run the app in the dev mode without duplicated beans?
pixel
  • 24,905
  • 36
  • 149
  • 251
  • Are the `BeansInitializer` in your main method and listed under `context.initializer.classes` that same class? If so, it's to be expected that the initializer will be called twice as you've configured things such that that's what will happen. – Andy Wilkinson Jun 22 '23 at 13:49
  • I know that this is expected, I look for a way to make it called once for @SpringBootTest and once for application in the dev mode – pixel Jun 28 '23 at 11:41

1 Answers1

0

The solution I found is to remove the initializer from application.yml in tests and move it to the @SpringBootTest annotation.

@SpringBootTest(properties = ["context.initializer.classes=com.myApp.BeansInitializer"])

This makes a perfect fit for both integration tests and running Testcontainers powered Spring Boot app

pixel
  • 24,905
  • 36
  • 149
  • 251