5

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?

user6123723
  • 10,546
  • 18
  • 67
  • 109
  • 1
    Make sure the import is static. import static org.springframework.test.web.server.request.MockMvcRequestBuilders.get; – Sparticles Jan 20 '14 at 08:21
  • @Sparticles So I did try putting in those static imports but the IDE can't seem to be able to find it (locally or in a Maven repo). Does it mean that I need to manually copy over these static classes to my project? That doesn't feel right! – user6123723 Jan 22 '14 at 17:34
  • Your missing the jars, here is a link to maven repo, download the appropriate one and add it to your library. http://mvnrepository.com/artifact/org.springframework/spring-test – Sparticles Jan 24 '14 at 03:32
  • @Sparticles I do have the spring-test dependency included. What I need here is MockHttpServletRequestBuilderTests.java which is not in spring-test jar. – user6123723 Jan 24 '14 at 04:29
  • What is the spring version? This feature needs 3.2+ if I'm not mistaken. – Yugang Zhou Jan 27 '14 at 01:17
  • @Hippoom I've been using 3.2.4.RELEASE. That is why I'm surprised! – user6123723 Jan 27 '14 at 04:22
  • org.springframework.test.web.servlet.request.MockMvcRequestBuilders – Dayong Mar 20 '15 at 14:01

3 Answers3

7

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.

vdenotaris
  • 13,297
  • 26
  • 81
  • 132
Matthew M
  • 111
  • 1
  • 3
4

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/

1

do you have

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

somewhere in your pom.xml?

hi_my_name_is
  • 4,894
  • 3
  • 34
  • 50