Search code examples
javaspring-mvcspring-test-mvc

java.lang.AssertionError: No ModelAndView found using webAppContextSetup


I wanted to test this controller:

@Controller
@RequestMapping(value = "/bookmanagement")
public class BookManagementController {

    @RequestMapping(value = "")
    public String bookManagement(HttpSession session, HttpServletRequest request, Model model, Authentication auth) {

        // does stuff
        return "bookManagement";
    }

    @RequestMapping(value = "edit")
    public String edit(HttpSession session, HttpServletRequest request, Model model, Authentication auth,
            @RequestParam(value = "id", required = true) String id) {

        // does stuff

        return "editBook";
    }

    @RequestMapping(value = "fileUpload", method = RequestMethod.POST)
    public String fileUpload(@RequestParam("coverFile") MultipartFile file,
            @RequestParam(value = "type", required = true) String type,
            @RequestParam(value = "id", required = true) String id,
            @RequestParam(value = "pages", required = true) String pages, HttpServletResponse response, Model model)
            throws JSONException {

        // writes file

        return "editBook_File";

    }


}

Please notice the RequestMapping. So this is my JUnit test:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration("file:application/webapp")
@ContextConfiguration("file:application/webapp/WEB-INF/dispatcher-servlet.xml")
public class BookManagementControllerTest {

    private MockMvc mockMvc;

    @Autowired
    private UsersRepository usersCollection;

    @Resource
    private FilterChainProxy springSecurityFilterChain;

    @Resource
    private WebApplicationContext webApplicationContext;

    private MockHttpSession session;

    @InjectMocks
    private BookManagementController bookManagementController;

    @Before
    public void setup() {

        MockitoAnnotations.initMocks(this);

        this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
                .addFilter(springSecurityFilterChain)
                .build();

        this.session = getSessionWithAuthentication();

    }

    @Test
    public void editTestView() throws Exception {

        this.mockMvc.perform(get("/bookmanagement/edit?id=58385d39bd7f3513c83faa65").session(session))
                .andExpect(view().name("editBook")).andExpect(status().isOk()).andReturn();

    }

    @Test
    public void uploadCoverFile() throws Exception {

        MockMultipartFile jsonFile = new MockMultipartFile("json", "", "application/json",
                "{\"json\": \"someValue\"}".getBytes());

        mockMvc.perform(MockMvcRequestBuilders.fileUpload("/bookmanagement/fileUpload").file(jsonFile))
                .andExpect(view().name("editBook_File")).andExpect(status().isOk()).andReturn();

    }

    private MockHttpSession getSessionWithAuthentication() {
        Users testPodUser = usersCollection.findByEmail("[email protected]");
        SecUserDetails principal = new SecUserDetails(testPodUser);
        SecurityContext secContext = SecurityContextHolder.getContext();
        secContext.setAuthentication(new UsernamePasswordAuthenticationToken(principal, principal.getPassword(),
                principal.getAuthorities()));

        MockHttpSession session = new MockHttpSession();
        session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, secContext);

        return session;
    }

}

The dispatcher-servlet.xml contains a component scan in the package in which the controller is in. When I manually test the controller, everything works fine.

So the first Test (editTestView()) returns green. But the second one fails with this error:

java.lang.AssertionError: No ModelAndView found
    at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:39)
    at org.springframework.test.util.AssertionErrors.assertTrue(AssertionErrors.java:72)
    at org.springframework.test.web.servlet.result.ViewResultMatchers$2.match(ViewResultMatchers.java:68)
    at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:171)
    at de.mypackage.prod.controller.BookManagementControllerTest.uploadCoverFile(BookManagementControllerTest.java:85)
    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:254)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
    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:193)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

I don't understand why the get("/bookmanagement/edit?id=58385d39bd7f3513c83faa65") call works fine but the fileUpload("/bookmanagement/fileUpload") on the other hand wouldn't.

What am I missing here?


Solution

  • It failed because ,

    Expected name of the file is coverFile,

    @RequestMapping(value = "fileUpload", method = RequestMethod.POST)
        public String fileUpload(@RequestParam("coverFile") MultipartFile file,
                @RequestParam(value = "type", required = true) String type,
                @RequestParam(value = "id", required = true) String id,
                @RequestParam(value = "pages", required = true) String pages, HttpServletResponse response, Model model)
                throws JSONException {
    

    But you are passing name as json,

    MockMultipartFile jsonFile = new MockMultipartFile("json", "", "application/json",
                    "{\"json\": \"someValue\"}".getBytes());
    

    You need to fix that as,

    MockMultipartFile jsonFile = new MockMultipartFile("coverFile", "", "application/json",
                        "{\"json\": \"someValue\"}".getBytes());
    

    Also check for other required parameters for the handler method.