Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@WebAppConfiguration and @ContextConfiguration for Spring Boot + Thymeleaf

Given a Spring Boot + Thymeleaf web application (this is nearly identical to the Spring project's gs-consuming-rest "initial" code tree):

├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── hello
    │   │       ├── Application.java
    │   │       ├── Config.java
    │   │       └── Controller.java
    │   └── resources
    │       └── templates
    │           └── index.html
    └── test
        └── java
            └── hello
                └── ControllerTest.java

...the user is greeted just fine with "Hello World!" at http://localhost:8080/, but the wiring of Spring's "context" does not seem to apply within the integration test (ControllerTest.java):

java.lang.AssertionError: Status 
Expected :200
Actual   :404

What is wrong with the project layout, and/or the configuration annotations in the test?

src/main/webapp/ is intentionally missing, along with things like web.xml and WEB-INF/. The goal here is to use minimal configuration, with an integration test to test-drive the development of the view and controller of the application.

Gory details below. Sorry in advance for the "wall of text."


pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.1.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
</dependencies>

Application.java

package hello;

// ...

@SpringBootApplication
public class Application {

  public static void main(String[] args) throws Throwable {
    SpringApplication.run(Application.class, args);
  }
}

Controller.java

package hello;

@org.springframework.stereotype.Controller
public class Controller {
}

Config.java

package hello;

// ...

@Configuration
public class Config extends WebMvcConfigurerAdapter {

  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("index");
  }
}

ControllerTest.java

package hello;

// ...

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = Config.class)
public class ControllerTest {

  @Autowired
  private WebApplicationContext wac;

  private MockMvc mockMvc;

  @Before
  public void setup() {
    mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
  }

  @Test
  public void test() throws Exception {
    this.mockMvc
        .perform(get("/"))
        .andExpect(status().isOk());
  }
}

index.html

<!DOCTYPE html>
<html>
  <head>
    <title>Hello World!</title>
  </head>
  <body>
    <p>Hello world!</p>
  </body>
</html>
like image 532
Tommy Stanton Avatar asked Jan 05 '16 08:01

Tommy Stanton


1 Answers

Thanks to @M.Deinum for helping me to realize that:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = Config.class)
public class ControllerTest {

...should be:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringApplicationConfiguration(classes = Application.class)
public class ControllerTest {

I suppose that @ContextConfiguration is meant for integration tests in Spring, whereas @SpringApplicationConfiguration is meant for integration tests in Spring Boot.

According to the Javadoc for the latter:

Class-level annotation that is used to determine how to load and configure an ApplicationContext for integration tests.

Similar to the standard @ContextConfiguration but uses Spring Boot's SpringApplicationContextLoader.

like image 194
Tommy Stanton Avatar answered Oct 12 '22 07:10

Tommy Stanton