Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I put TestConfiguration in an abstract class?

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?

like image 527
jd96 Avatar asked Oct 30 '25 03:10

jd96


1 Answers

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 {
      ------
}
like image 109
SauriBabu Avatar answered Oct 31 '25 16:10

SauriBabu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!