在 TestNG 上更改 JUnit,NullPointerException
Change JUnit on TestNG, NullPointerException
我尝试将项目中的测试框架从 JUnit 更改为 TestNG。这段代码,即测试控制器,可以正常工作:
@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();
}
}
}
据我所知,在 testNg,我应该从 AbstractTestNGSpringContextTest 扩展我的测试 class 并删除带有 @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);
}
...
...
但是有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.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)
我做错了什么?
添加注释@TestExecutionListeners(MockitoTestExecutionListener.class)
我尝试将项目中的测试框架从 JUnit 更改为 TestNG。这段代码,即测试控制器,可以正常工作:
@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();
}
}
}
据我所知,在 testNg,我应该从 AbstractTestNGSpringContextTest 扩展我的测试 class 并删除带有 @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);
}
...
...
但是有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.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)
我做错了什么?
添加注释@TestExecutionListeners(MockitoTestExecutionListener.class)