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.
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>
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)}
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?
Also note that we can wire other spring beans in our jUnit test classes using @Autowired annotation.
if you are writing unit tests a recommend you use @Mock and @InjectMocks . But if you really want test all the flow and need to inject classes, you can @RunWith(SpringJUnit4ClassRunner. class) and @Autowired your classes.
Spring Boot - Using @TestConfiguration to define beans for tests. In Spring Boot, @TestConfiguration annotation can be used to define/override beans for unit tests.
You can annotate a test class with @WebAppConfiguration
to instruct Spring's test framework to create a web application context (which is sufficient for autowiring ErrorAttributes
) without actually starting the embedded container.
Annotate your test base class or individual test case classes with @WebIntegrationTest
It's mentioned here:
http://docs.spring.io/spring-boot/docs/1.2.1.RELEASE/reference/htmlsingle/#boot-features-testing-spring-boot-applications
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