Change JUnit on TestNG, NullPointerException

petrov.aleksandr :

I try to change testing framework in my project from JUnit to TestNG. This code, that test controller, work correctly:

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {DependencyReportController.class, DependencyReportControllerTest.SecurityPermitAllConfig.class})
@WebMvcTest(controllers = DependencyReportController.class)

public class DependencyReportControllerTest {
    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private DependencyDifferenceService differenceService;

    @Before
    public void before() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void test() throws Exception {
        ServiceDependenciesReport report = new ServiceDependenciesReport();
        report.setElapsed("elapsed");
        report.setSuccess(true);

        List<ByService> byServices = new ArrayList<>();
        byServices.add(new ByService("service1", new ArrayList<>()));
        byServices.add(new ByService("service2", new ArrayList<>()));
        byServices.add(new ByService("service3", new ArrayList<>()));

        report.setByServices(byServices);
        when(differenceService.getAllDiffs()).thenReturn(report);

        this.mockMvc.perform(get("dependencies/difference")).andExpect(status().isOk())
            .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON_UTF8_VALUE))
            .andExpect(jsonPath("$.success", is(true)))
            .andExpect(jsonPath("$.byServices", hasSize(1)))
            .andExpect(jsonPath("$.byServices[0].serviceName", is("service1")))
    }

    @Configuration
    public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests().anyRequest().permitAll()
                .and().csrf().disable();
        }
    }    
}

As I know, at testNg I should extend my test class from a AbstractTestNGSpringContextTest and delete line with @RunWith(SpringRunner.class):

    @ContextConfiguration(classes = {DependencyReportController.class, DependencyReportControllerTest.SecurityPermitAllConfig.class})
    @WebMvcTest(controllers = DependencyReportController.class)

        public class DependencyReportControllerTest extends AbstractTestNGSpringContextTest {
            @Autowired
            private MockMvc mockMvc;

            @MockBean
            private DependencyDifferenceService differenceService;

            @BeforeTest
            public void before() {
                MockitoAnnotations.initMocks(this);
            }
...
...

But there are NPE:

java.lang.NullPointerException at report.controller.DependencyReportControllerTest.test(DependencyReportControllerTest.java:61) 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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133) at org.testng.internal.MethodInvocationHelper$1.runTestMethod(MethodInvocationHelper.java:239) at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.run(AbstractTestNGSpringContextTests.java:180) at org.testng.internal.MethodInvocationHelper.invokeHookable(MethodInvocationHelper.java:251) at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:580) at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:172) at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46) at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:804) at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:145) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128) at java.util.ArrayList.forEach(ArrayList.java:1257) at org.testng.TestRunner.privateRun(TestRunner.java:770) at org.testng.TestRunner.run(TestRunner.java:591) at org.testng.SuiteRunner.runTest(SuiteRunner.java:402) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355) at org.testng.SuiteRunner.run(SuiteRunner.java:304) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180) at org.testng.TestNG.runSuitesLocally(TestNG.java:1102) at org.testng.TestNG.runSuites(TestNG.java:1032) at org.testng.TestNG.run(TestNG.java:1000) at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:73) at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)

What am I doing wrong?

Jeg1337 :

add annotation @TestExecutionListeners(MockitoTestExecutionListener.class)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=422540&siteId=1