-1

I wrote this integration test and i have nullpointer exception but i can not fix it. I will copy the stack trace. I think that the problem comes from this line - .andExpect(view().name("/login")) but i do not find information in spring docs.

public class IntegratonTest {

    private MockMvc mockMvc;

    @Before
    public void setup() throws Exception {

        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WebContent/j/");
        viewResolver.setSuffix(".jsp");
        this.mockMvc = standaloneSetup(new LoginController()).setViewResolvers(viewResolver).build();
    }

    @Test
    public void testShowForm() throws Exception {
        this.mockMvc.perform(get("/login"))
                    .andExpect(status().isOk())
                    .andExpect(view().name("/login"))
                    .andExpect(model().attribute("login", hasProperty("email", isEmptyOrNullString())))
                    .andExpect(model().attribute("login", hasProperty("password", isEmptyOrNullString())));
    }

    @Test
    public void testOnSubmitLogin() throws Exception {
        this.mockMvc.perform(post("/login")
                    .param("email", "email@email.com")
                    .param("password", "password"))
                    .andExpect(status().isOk()).andExpect(redirectedUrl(null));
    }

}

Stack Trace

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:65)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:167)
    at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134)
    at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:144)
    at test.loginController.IntegratonTest.testShowForm(IntegratonTest.java:38)
    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:497)
    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.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    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.junit.runners.ParentRunner.run(ParentRunner.java:363)
    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)
Caused by: java.lang.NullPointerException
    at bg.profiles.controller.LoginController.showForm(LoginController.java:71)
    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:497)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    ... 33 more

EDIT: @Simze i do not ask what is a nullpointer. I asked how to fix the nullpointer that i have because it may be cause from a incorrect path or error in the controller class etc.

EDIT1:

@Mock
    private LoginController logController;

    @Before
    public void setup() throws Exception {
        logController = new LoginController();
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WebContent/j/");
        viewResolver.setSuffix(".jsp");
        this.mockMvc = standaloneSetup(logController).setViewResolvers(viewResolver).build();
    }
RockOrDead
  • 340
  • 4
  • 17

1 Answers1

1

You cannot create instance of Spring Component by calling "new", because all injected beans to your LogicController won't be autowired. You must create instance of LogicController in a "spring" way. That's why your msa is null there.

szymon_prz
  • 540
  • 3
  • 14
  • i do not use a configuration file for now. how to make the instance ? – RockOrDead Feb 17 '16 at 12:28
  • You have to write minimal configuration context file [link](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html). There you can configure only this component what you're using in this test. Because this is an integration test you must define all component used in this test. But there is a workaround.You can mock some of components by using springockito [link](https://bitbucket.org/kubek2k/springockito/wiki/Home) – szymon_prz Feb 17 '16 at 12:35
  • I mocked LoginController but still have the same exception. – RockOrDead Feb 17 '16 at 12:41
  • how you mocked loginController? – szymon_prz Feb 17 '16 at 12:57
  • i edited my question. – RockOrDead Feb 17 '16 at 13:01
  • If you mocked your controller this wouldn't be an integration test. Anyway to make this work delete this line ` logController = new LoginController();` next use a Mockito.when() here's the examples [link](https://gojko.net/2009/10/23/mockito-in-six-easy-examples/). And you cannot @Mock your Controller you have to use @InjectMocks and @Mock your msa – szymon_prz Feb 17 '16 at 13:11