经过@Bobernac Alexandru的审查和提议后更新
该问题已随提案更新,您可以直接使用;)
运行Springboot集成测试时遇到问题。
我的考试班
public class DepenseEndpointIT extends AbstractIT {
private DonneesTestEndpointUtil donneeTest = new DonneesTestEndpointUtil();
@Override
@Before
public void setUp() {
System.setProperty("jwt", false)
super.setUp();
}
@Test
public void shouldReturnVentePrevHttpStatus200() throws Exception {
String uri = "/agregats";
MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(uri).params(params).accept(MediaType.APPLICATION_JSON_UTF8_VALUE)).andReturn();
int status = mvcResult.getResponse().getStatus();
String responseContent = mvcResult.getResponse().getContentAsString();
}
}
AbstractIT看起来像(我已替换为更多的可读性包名称)
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DepenseEndpoint.class,
properties = {"spring.cloud.config.enabled=false", "spring.cloud.zookeeper.enabled=false", "spring.cloud.zookeeper.discovery.enabled"})
@EntityScan({"si.data.analytics.ven.history.entity", "si.data.analytics.ven.history.domain"})
@WebAppConfiguration
@ComponentScan(basePackages = {"si.data.analytics.ven"})
public abstract class AbstractIT {
protected MockMvc mvc;
@Autowired
WebApplicationContext webApplicationContext;
protected void setUp() {
mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
}
运行测试时,出现以下错误,在执行mvn.perform(...)
时抛出了异常
org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常为 java.util.concurrent.ExecutionException:java.awt.HeadlessException
在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1013) 在 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:645)在 org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) 在 org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:71) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:750)在 org.springframework.mock.web.MockFilterChain $ ServletFilterProxy.doFilter(MockFilterChain.java:166) 在 org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:133) 在 org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:182) 在 com.snef.si.data.analytics.ven.integration.DepenseEndpointIT.shouldReturnVentePrevHttpStatus200(DepenseEndpointIT.java:50) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:498)在 org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在 org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74) 在 org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84) 在 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 在 org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) 在 org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) 在 org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) 在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)处 org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251) 在 org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) 在org.junit.runners.ParentRunner处$ 3.run(ParentRunner.java:290)在 org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)在 org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)在 org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)在 org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 在 org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) 在org.junit.runners.ParentRunner.run(ParentRunner.java:363)处 org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) 在org.junit.runner.JUnitCore.run(JUnitCore.java:137)处 com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) 在 com.intellij.rt.execution.junit.IdeaTestRunner $ Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) 在 com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) 在 com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) 引起原因:java.util.concurrent.ExecutionException: java.awt.HeadlessException
在我的main()中,我已经修改了运行应用程序的方式,将headless
设置为false以避免该错误,并且它工作正常,但是我的集成测试无效:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class})
@ComponentScan(basePackages = {"si.data.analytics.ven", "si.data.analytics.foundation.logging.aspect"})
@EnableAsync
@EnableDiscoveryClient
public class Main {
public static void main(String[] args) {
SpringApplicationBuilder builder = new SpringApplicationBuilder(Main.class);
builder.headless(false).run(args);
}
}
有什么我想念的吗?有什么方法可以传递相同的参数,即builder.headless(false)
来测试类?
预先感谢您的帮助
答案 0 :(得分:1)
您在两个类中都添加了@RunWith。从@RunWith(SpringRunner.class)
中删除DepenseEndpointIT
,并随着AbstractIT
扩展SpringRunner
将其添加到SpringJUnit4ClassRunner
中。应该可以。
答案 1 :(得分:1)
我找到了解决此问题的解决方案,方法是设置java.awt.headless
到false
@Override
@Before
public void setUp() {
System.setProperty("java.awt.headless", "false");
super.setUp();
}