1
votes

Updates after review and proposal from @Bobernac Alexandru

The question is updated with proposals, you can just use it directly ;)

I have an issue while running Springboot integration tests.

My test 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 looks like (I replaced for more readability package names)

@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();
    }
}

When I run my tests, I have the following error, the exception is thrown, while doing 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$1.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$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.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

In my main(), I have modified the way I run my application, I set headless to false to avoid the error, and it's working fine, but my integration test is not working:

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

Is there something that I have missed ? Is there any way we can pass the same paramteter, i.e: builder.headless(false) to test classes ?

Thanks in advance for your help

2

2 Answers

1
votes

You added @RunWith in both classes. Remove @RunWith(SpringRunner.class) from DepenseEndpointIT and add it to AbstractIT as SpringRunner extends SpringJUnit4ClassRunner. Should work.

1
votes

I found a solution to overcome the issue, by setting java.awt.headless to false

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