Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring4 MVC Unit test does not compile

I have strange behaviour when trying to compile sources after Spring 3.2.5 → 4.0.0 version update.

Faulty code snippet from ApplicationControllerTest.java (it is equivalent to code from documentation):

import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
...
@Autowired
private WebApplicationContext wac;

private MockMvc               mockMvc;

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

Error:

COMPILATION ERROR : /C:/Development/.../war/src/test/java/org/.../web/controller/ApplicationControllerTest.java:[59,61] C:\Development\...\war\src\test\java\org\...\web\controller\ApplicationControllerTest.java:59: incompatible types; inferred type argument(s) java.lang.Object do not conform to bounds of type variable(s) B found : <B>org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder<B> required: java.lang.Object

If one looks into MockMvcBuilders sources, one can see the difference:

Spring 4.0.0:

public static <B extends DefaultMockMvcBuilder<B>> DefaultMockMvcBuilder<B> webAppContextSetup(WebApplicationContext context) {
    return new DefaultMockMvcBuilder<B>(context);
}

Spring 3.2.5:

public static DefaultMockMvcBuilder<DefaultMockMvcBuilder<?>> webAppContextSetup(WebApplicationContext context) {
    return new DefaultMockMvcBuilder<DefaultMockMvcBuilder<?>>(context);
}

My attempts to make it compilable did not succeeded.

Actually Spring documentation says that framework should be Java 1.6.0_10 compatible. I use Java 1.6.0_45.

like image 892
dma_k Avatar asked Dec 13 '13 19:12

dma_k


1 Answers

I think the call of webAppContextSetup method now should be explicitly parameterized with the class of <B extends DefaultMockMvcBuilder<B>>. The obvious candidates are StandaloneMockMvcBuilder or simply DefaultMockMvcBuilder (though the later will generate a warning about unchecked or unsafe operations). So try this:

mockMvc = MockMvcBuilders.<StandaloneMockMvcBuilder>webAppContextSetup(wac).build();
like image 121
Eugene Evdokimov Avatar answered Sep 22 '22 18:09

Eugene Evdokimov