Spring integration testing controller with dependency injection

I am trying to write an integration test for one of my controller classes that has a nested dependency. I try to check part of my controller when it calls a method through an injected object, but when I run my test, it crashes due to a null pointer exception. In the test, I used @ContexConfiguration and @RunsWith annotations, but this did not help. Some code might help :)

AuthenticationController:

@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"; } } 

in test-context.xml: beans:bean id="userManagement" class="my.packages.UserManagement"

AuthenticationControllerTest:

 @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"); } 

The column tells me that the error occurs where the test calls the login method for the entered userMangaement object. um = null, so the injection does not work with the test. The controller works fine when used.

Any comment will help a lot!

Thanks in advance,

Sorex

+4
source share
1 answer

If you want to use autowire dependencies, you cannot create your controller like this:

 controller = new AuthenticationController(); 

You can authorize your addiction in your test.

 @Autowired private UserManagement um; 

and create a constructor in the controller to be able to do this:

 @Before public void setUp() { controller = new AuthenticationController(um); } 

But I would recommend using a MockServletContext.

 MockServletContext mockServletContext = new MockServletContext(); mockServletContext.addInitParameter("contextConfigLocation", "path to your xml config")); ContextLoaderListener listener = new ContextLoaderListener(); listener.initWebApplicationContext(mockServletContext); 

There should also be a link to the DispatcherServlet. I have never done this in the m servlet environment, only in the spring mvc portlet, but this shoul will be similar. The idea is to create a fake web application context and call dispatch servlet to fully test the integration between spring configuration controllers.

+4
source

Source: https://habr.com/ru/post/1398813/


All Articles