Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Controller Testing. Can't seem to find MockMvcRequestBuilders

I'm trying to write some Unit and Integration tests for my Spring Controllers following this guide and Spring's documentation for testing MVC controllers

The problem is that I'm unable to find the appropriate includes in mvnrepository for the following piece of code

 this.mockMvc.perform(get("/foo").accept("application/json"))
        .andExpect(status().isOk())
        .andExpect(content().mimeType("application/json"));

I'm unable to find the jar for get("/foo) method and .mimeType(....).

Upon googling, I was however able to find out the source for the above get and mimeType at here. So, should I just copy paste these helper classes from this Spring Test showcase project? or am I missing something here?

like image 974
user6123723 Avatar asked Jan 20 '14 03:01

user6123723


3 Answers

Looks like the package name changed from test.web.server to test.web.servlet in spring-test and the blog articles/docs are out of date for Spring 4.

like image 74
Matthew M Avatar answered Oct 22 '22 22:10

Matthew M


I'm assuming you're using Eclipse IDE. Unfortunately it doesn't automatically import static imports.

You have to add these through: Window > Preferences > Java > Editor > Content Assist > Favorites.

Here's a good post with more information:
http://piotrnowicki.com/2012/04/content-assist-with-static-imports-in-eclipse/

like image 31
Erik Markhauser Avatar answered Oct 22 '22 23:10

Erik Markhauser


do you have

 <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
    </dependency>

somewhere in your pom.xml?

like image 1
hi_my_name_is Avatar answered Oct 22 '22 21:10

hi_my_name_is