Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get ErrorAttributes autowired also in tests?

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?

like image 539
Jonik Avatar asked Feb 23 '15 10:02

Jonik


People also ask

Can you Autowire in a jUnit?

Also note that we can wire other spring beans in our jUnit test classes using @Autowired annotation.

Which type of tests use @autowired?

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.

How Bean is defined in spring boot test?

Spring Boot - Using @TestConfiguration to define beans for tests. In Spring Boot, @TestConfiguration annotation can be used to define/override beans for unit tests.


2 Answers

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.

like image 198
Andy Wilkinson Avatar answered Nov 15 '22 04:11

Andy Wilkinson


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

like image 23
ci_ Avatar answered Nov 15 '22 05:11

ci_