This is probably quite basic, but I'm new to Spring Boot (and many aspects of Spring in general) and the documentation didn't directly answer this.
The setup
Using latest Spring Boot (1.2.1), I have some integration tests where Spring is loaded up and dependencies nicely autowired (it was delightfully simple to set this up).
Base class for tests:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public abstract class IntegrationTest {
}
The main Application
class doesn't have much more than main method with SpringApplication.run()
and these annotations:
@ComponentScan
@EnableAutoConfiguration
@EnableScheduling
Example test:
public class UserServiceTest extends IntegrationTest {
@Autowired
UserService userService;
@Test
public void testSomething() throws Exception {
// Use UserService; make assertions
}
}
For necessary dependiencies, I just have spring-boot-starter-test
:
<!-- Typical Spring Boot test dependencies: Spring Test, JUnit, Hamcrest, Mockito -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
The problem
I created a custom ErrorController along these lines, where I define an @Autowired ErrorAttributes
field. See the CustomErrorController source code.
After this, the Spring integration tests stopped working:
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [org.springframework.boot.autoconfigure.web.ErrorAttributes] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency.
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
The question
What is the simplest, cleanest way to get that ErrorAttributes bean injected also in tests?
Should I create separate Application
used for tests, with some kind of mocked ErrorAttributes
bean, or might there be a simpler way? Am I missing some helper or dependency related to web/controller testing?