Spring Boot 集成测试失败 java.awt.HeadlessException

SpringBoot integration tests failing with java.awt.HeadlessException

@Bobernac Alexandru 审核和提议后的更新

问题已更新为建议,您可以直接使用它;)

我在 运行 进行 Springboot 集成测试时遇到问题。

我的测试class

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: Request processing failed; nested exception is java.util.concurrent.ExecutionException: java.awt.HeadlessException

at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1013) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897) at javax.servlet.http.HttpServlet.service(HttpServlet.java:645) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:71) at javax.servlet.http.HttpServlet.service(HttpServlet.java:750) at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:166) at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:133) at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:182) at com.snef.si.data.analytics.ven.integration.DepenseEndpointIT.shouldReturnVentePrevHttpStatus200(DepenseEndpointIT.java:50) 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.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74) at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) at org.junit.runners.ParentRunner.run(ParentRunner.java:290) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access[=17=]0(ParentRunner.java:58) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) Caused by: 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) 来测试 classes ?

在此先感谢您的帮助

您在两个 类 中都添加了 @RunWith。从 DepenseEndpointIT 中删除 @RunWith(SpringRunner.class),并将其添加到 AbstractIT,因为 SpringRunner 扩展了 SpringJUnit4ClassRunner。应该可以。

我找到了解决问题的方法,设置 java.awt.headlessfalse

@Override
@Before
public void setUp() {
    System.setProperty("java.awt.headless", "false");
    super.setUp();
}