Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

testing spring-boot web-app with thymeleaf

I am trying to write tests to make sure that my controllers loads my views.

When doing this i get an "circular view path exception". This is due to the thymeleaf-view-resolver not beeing present.

A simple controller-method looks like this:

@Cacheable("Customers")
@RequestMapping(value="/customer",  method = RequestMethod.GET)
public String customer(Model model) {
    model.addAttribute("customer", "customer");
    return "customer";
}

My views are located on src/main/resources/templates (autoconfig by spring-boot) and in this exapmle the view is named customer.html. If i change the view-name to be different from the value of @requestMapping, then i avoid the error ofc.

How can i provide the ThymeleafViewResolver that Spring-boot-autoconfig creates to my tests?

This question states that i have to do it, but doesnt say how..: How to avoid the "Circular view path" exception with Spring MVC test

CustomerControllerTest.java

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

    @Autowired
    CustomerController customerController;

    private MockMvc mockMvc;

    @Before
    public void setup(){
        // Process mock annotations
        MockitoAnnotations.initMocks(this);

        // Setup Spring test in stand-alone mode
        this.mockMvc = MockMvcBuilders.standaloneSetup(customerController).build();
    }

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

Exception

javax.servlet.ServletException: Circular view path [customer]: would dispatch back to the current handler URL [/customer] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
    at org.springframework.web.servlet.view.InternalResourceView.prepareForRendering(InternalResourceView.java:263)
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:186)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:266)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1225)
    at org.springframework.test.web.servlet.TestDispatcherServlet.render(TestDispatcherServlet.java:119)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1012)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
    at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:64)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:170)
    at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:137)
    at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:141)
    at com.***.***.salesweb.web.controller.CustomerControllerTest.testLoadCustomerPage(CustomerControllerTest.java:51)

Thanks for all replies in advance ppl!

like image 880
Jørgen Skår Fischer Avatar asked Jan 27 '14 08:01

Jørgen Skår Fischer


People also ask

How do I test a spring boot rest controller?

Spring Boot provides an easy way to write a Unit Test for Rest Controller file. With the help of SpringJUnit4ClassRunner and MockMvc, we can create a web application context to write Unit Test for Rest Controller file.

How do I test a spring boot unit?

Then, configure the Application context for the tests. The @Profile(“test”) annotation is used to configure the class when the Test cases are running. Now, you can write a Unit Test case for Order Service under the src/test/resources package. The complete code for build configuration file is given below.


1 Answers

After the brilliant comments here i changed the controller to the following, and it now works :)

CustomerControllerTest.java

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

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

    @Autowired
    CustomerController customerController;

    private MockMvc mockMvc;

    @Autowired
    WebApplicationContext wac;

    @Before
    public void setup(){
    
        // Process mock annotations
        MockitoAnnotations.initMocks(this);

        // Setup Spring test in webapp-mode (same config as spring-boot)
        this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    @Test
    public void testLoadCustomerPage() throws Exception{
        this.mockMvc.perform(get("/customer")).andExpect(status().isOk());  
    }
}
like image 51
Jørgen Skår Fischer Avatar answered Oct 10 '22 05:10

Jørgen Skår Fischer