gpt4 book ai didi

java - 更改 TestNG 上的 JUnit、NullPointerException

转载 作者:行者123 更新时间:2023-11-30 01:41:03 26 4
gpt4 key购买 nike

我尝试将项目中的测试框架从 JUnit 更改为 TestNG。这段代码,即测试 Controller ,工作正常:

@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 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)

我做错了什么?

最佳答案

添加注释@TestExecutionListeners(MockitoTestExecutionListener.class)

关于java - 更改 TestNG 上的 JUnit、NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59965587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com