19
Июл
2017

Тестирование Spring-test через MockMvc

Добрый день, пытаюсь создать тест для web уровня приложения, есть некий запрос

MockHttpServletRequestBuilder request = MockMvcRequestBuilders.get("/URL?params");
this.mockMvc.perform(request).andExpect(ok);

Начало теста

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class ControllersTest {
@Autowired
WebApplicationContext wac;

MockMvc mockMvc;

@Before
public void setup() {
    DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(this.wac);
    this.mockMvc = builder.build();
}

Ну и собственно далее пошел тест.

Но я никак не могу понять, почему он мне возвращает 404 ошибку. То есть ожидается 200, а actual 404. Как сделать так, чтобы 200 вернул? Может я что-то упустил?

вот стек, но не знаю, даст ли он что-нибудь

java.lang.AssertionError: Status 
Expected :200
Actual   :404
<Click to see difference>


at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:81)
at org.springframework.test.web.servlet.result.StatusResultMatchers$10.match(StatusResultMatchers.java:665)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:171)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Контроллер

@Controller
@RequestMapping("/read/object-attributes")
public class GroupAttributeReadController {

@Autowired
private GroupAttributeService groupAttributeService;
@RequestMapping(value = "/import", method = RequestMethod.GET)
public
@ResponseBody
GroupAttributeBufferListResponse findAll(@RequestParam(value = "pageNum", required = true) int pageNum,
                                         @RequestParam(value = "pageSize", required = true) int pageSize,
                                         @RequestParam(value = "order", required = false) String order,
                                         @RequestParam(value = "orderDir", required = false) String orderDir,
                                         @RequestParam(value = "loadSession") Long loadSession,
                                         @RequestParam( value = "showCorrect", defaultValue = "0") Integer showCorrect,
                                         @RequestParam(value="naviUser") String user,
                                         @RequestParam Map<String, String > params,
                                         HttpServletResponse response, Locale locale) {

}

@RequestMapping(value = "/download", method = RequestMethod.GET)
public void download(@RequestParam(value = "loadSession") Long loadSession,
                     @RequestParam(value = "showCorrect", defaultValue = "0") Integer showCorrect,
                     @RequestParam Map<String, String > params,
                     @RequestParam(value="naviUser") String user,
                     HttpServletResponse response, Locale locale) throws IOException {

}

@RequestMapping(value = "/uncommit-session", method = RequestMethod.GET)
public
@ResponseBody
Page<PrmGroupAttributeLoadSession> getAllSession(@RequestParam(value = "fll_id") Long fllId,
                                                 @RequestParam(value = "pageSize") Integer pageSize,
                                                 @RequestParam(value = "pageNum") Integer pageNum,
                                                 @RequestParam(value="naviUser") String user,
                                                 Locale locale) {

}
}

Тест

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class ControllersTest {
@Autowired
WebApplicationContext wac;

MockMvc mockMvc;

@Before
public void setup() {
    DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(this.wac);
    this.mockMvc = builder.build();
}

@Test
public void testController() throws Exception {
    ResultMatcher ok = MockMvcResultMatchers.status().isOk();

    MockHttpServletRequestBuilder request = MockMvcRequestBuilders.get("/read/object-attributes/import?pageNum=2&pageSize=5&order=test&orderDir=DESC&loadSession=1&showCorrect=0&naviUser=user&FILTER_Test=Test");
    this.mockMvc.perform(request)
            .andExpect(ok);

    request = MockMvcRequestBuilders.get("/read/object-attributes/download?loadSession=1&showCorrect=0&&FILTER_Test=Test&naviUser=user");
    this.mockMvc.perform(request)
            .andExpect(ok);

    request = MockMvcRequestBuilders.get("/read/object-attributes/uncommit-session?fll_id=1&pageSize=5&pageNum=2&naviUser=user");
    this.mockMvc.perform(request)
            .andExpect(ok);

    }
}

Источник: https://ru.stackoverflow.com/questions/694058/%D0%A2%D0%B5%D1%81%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5-spring-test-%D1%87%D0%B5%D1%80%D0%B5%D0%B7-mockmvc

Share

Тебе может это понравится...