【发布时间】:2020-01-29 10:56:43
【问题描述】:
我尝试将项目中的测试框架从 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 扩展我的测试类,并使用 @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 在 report.controller.DependencyReportControllerTest.test(DependencyReportControllerTest.java:61) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:498) 在 org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133) 在 org.testng.internal.MethodInvocationHelper$1.runTestMethod(MethodInvocationHelper.java:239) 在 org.springframework.test.context.testng.AbstractTestNGSpringContextTests.run(AbstractTestNGSpringContextTests.java:180) 在 org.testng.internal.MethodInvocationHelper.invokeHookable(MethodInvocationHelper.java:251) 在 org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:580) 在 org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:172) 在 org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46) 在 org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:804) 在 org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:145) 在 org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146) 在 org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128) 在 java.util.ArrayList.forEach(ArrayList.java:1257) 在 org.testng.TestRunner.privateRun(TestRunner.java:770) 在 org.testng.TestRunner.run(TestRunner.java:591) 在 org.testng.SuiteRunner.runTest(SuiteRunner.java:402) 在 org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396) 在 org.testng.SuiteRunner.privateRun(SuiteRunner.java:355) 在 org.testng.SuiteRunner.run(SuiteRunner.java:304) 在 org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53) 在 org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96) 在 org.testng.TestNG.runSuitesSequentially(TestNG.java:1180) 在 org.testng.TestNG.runSuitesLocally(TestNG.java:1102) 在 org.testng.TestNG.runSuites(TestNG.java:1032) 在 org.testng.TestNG.run(TestNG.java:1000) 在 org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:73) 在 org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
我做错了什么?
【问题讨论】:
标签: java unit-testing testng