I have some Spring tests that all define the same test configuration:
@TestConfiguration
public static class TestConfig {
@Bean
public EmployeeClient employeeClient(MockMvc mockMvc) {
return new EmployeeClient(mockMvc);
}
}
To reduce replication I put that code into an abstract class and had each test extend that abstract class. However, this threw Spring unresolved dependencies - it couldn't resolve EmployeeClient.
How can I make this work?
One of the ways is to make your Test class use multiple configuration files.
@Configuration
class CommonBeans {
}
@Configuration
class SpecificBeans {
}
@ContextConfiguration(classes = {CommonBeans.class, SpecificBeans.class})
public class MyAppTest {
------
}
In your test package if you are creating a configuration using @TestConfiguration instead of @Configuration then you have to use @Import annotation to make use of that.
@TestConfiguration
public class TestBeans {
}
@Import(TestBeans.class)
public class MyAppTest {
------
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With