0

I'm getting an exception when trying to write a unit test for a simple LoginController method that returns a Thymeleaf view.

Sounds like I need to specify a ViewResolver but not sure how to do that.

LoginController

@Controller
public class LoginController {

    @RequestMapping(value = {"/", "/login"}, method = RequestMethod.GET)
    public ModelAndView login() {
      return new ModelAndView("login");
    }

}

LoginControllerTest

@RunWith(SpringRunner.class)
public class LoginControllerTest {
  private MockMvc mockMvc;

  @Before
  public void setUp() {

    mockMvc = MockMvcBuilders.standaloneSetup(new LoginController())
                             .build();

  }

  @Test
  public void testLoginPage() throws Exception {
    this.mockMvc.perform(get("/login"))
                .andExpect(status().isOk())
                .andExpect(view().name("login"))
                .andDo(print());
  }
}

Error

javax.servlet.ServletException: Circular view path [/login]: would dispatch back to the current handler URL [/login] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
A_B
  • 1,009
  • 3
  • 15
  • 37
  • "this.mockMvc.perform(get("/login"))" Are you sure about URL? – Pratik Ambani Mar 16 '17 at 16:21
  • Possible duplicate of [How to avoid the "Circular view path" exception with Spring MVC test](http://stackoverflow.com/questions/18813615/how-to-avoid-the-circular-view-path-exception-with-spring-mvc-test) – ryanp Mar 16 '17 at 18:34
  • http://stackoverflow.com/a/27797981/1143427 is a good solution for your particular use case (standaloneSetup & no assertions on view content) – ryanp Mar 16 '17 at 18:36
  • @ryanp thanks, that particular answer solved my problem – A_B Mar 16 '17 at 19:08
  • @ryanp thanks! The link which you have provided solved my problem! – SDB Aug 24 '21 at 07:19

0 Answers0