https://kotlinlang.org logo
Title
p

Philipp Mayer

05/23/2023, 8:12 PM
Hi all, hope you’re having a great day! I have a test as follows:
@SpringBootTest(classes = [TestConfig::class])
class OrderTests: OtherClass {
    val orders = listOf<Order>() //this is just a representation - it's actually part of OtherClass
    
    @Import(Application::class)
    class TestConfig {
        // I'd like to pass it in here
        @Bean
        fun orderRepository() = FakeOrderRepository(orders)
    }

    @Test
    fun `works now`() {
        println("yay")
    }
}
I have a Spring test where I’m replacing a bean. However, I’d like to supply a specific parameter (
orders
) when creating the bean. The problem is that
orders
is part of another class that is implemented by `OrderTests`:
OtherClass
. I played around with it and can’t think about a way of how I could pass that into the bean. The problem is that it’s quite crucial and that there’s no way to change the way the
orders
are set up in
OtherClass
. The easiest (from a vanilla point of view) would be to mark
TestConfig
as
inner class
, but that’s not compatible with Spring. Do you have an idea? Thanks in advance! 👋
a

Adam S

05/23/2023, 8:39 PM
1. add a function
FakeOrderRepository.setOrders(orders: List<Order>)
so the orders can be modified per test, instead of upon initialisation. 2. autowire the bean into the test class, so it’s available as a property 3. in a
@BeforeEach
function set the orders using
orderRepository.setOrders(orders)
Just a suggestion - there might be a better way