Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring integration testing a controller with dependency injection
    primarykey
    data
    text
    <p>I am trying ot write an integration test for one of my controller classes which have an injected dependency in it. I try to test the part of my controller where it's calling a method through the injected object, but when i run my test its failing due to a null pointer exception. At the test i used @ContexConfiguration and @RunsWith annotations, but it didin't helped. Some code might help :)</p> <p>AuthenticationController:</p> <pre><code>@Controller public class AuthenticationController { @Resource(name = "userManagement") private UserManagement um; @RequestMapping(method = RequestMethod.POST) public String onSubmit(@ModelAttribute("user") UserForm user, BindingResult result, Model model, HttpSession session) { LoginFormValidator validator = new LoginFormValidator(); validator.validate(user, result); if (result.hasErrors()) { return "login"; } else { User u = um.login(user.getEmail(), user.getPwd()); if (u != null) { session.setAttribute("user", u); LOGGER.info("succesful login with email: " + u.getEmail()); model.addAttribute("result", "succesful login"); } else { model.addAttribute("result", "login failed"); } return "result"; } } </code></pre> <p>in test-context.xml: <code>beans:bean id="userManagement" class="my.packages.UserManagement"</code></p> <p>AuthenticationControllerTest:</p> <pre><code>@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"test-context.xml" }) public class AuthenticationControllerTest { private MockHttpServletRequest request; private MockHttpServletResponse response; private AuthenticationController controller; @Before public void setUp() { request = new MockHttpServletRequest(); response = new MockHttpServletResponse(); controller = new AuthenticationController(); } @Test public void testLoginPost() throws Exception { request.setMethod("POST"); request.setRequestURI("/login"); request.setParameter("email", "test@email.com"); request.setParameter("pwd", "test"); final ModelAndView mav = new AnnotationMethodHandlerAdapter() .handle(request, response, controller); final UserForm u = assertAndReturnModelAttributeOfType(mav, "user", UserForm.class); assertEquals("test@email.com", u.getEmail()); assertEquals("test", u.getPwd()); assertViewName(mav, "result"); /* if UserForm is not valid */ final BindingResult errors = assertAndReturnModelAttributeOfType(mav, "org.springframework.validation.BindingResult.user", BindingResult.class); assertTrue(errors.hasErrors()); assertViewName(mav, "login"); } </code></pre> <p>The stacktrace tells me that the error happens where the test calls the login method of the injected userMangaement object. um = null so the injection is not working with the test. The controller works fine in useage.</p> <p>Any comment would help a lot!</p> <p>Thanks in advance,</p> <p>Sorex</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload