如何使用SpringBoot测试awt。*?

时间:2017-11-14 19:24:50

标签: java spring junit awt

我有一个产生屏幕截图的课程:

@Component
public class ImagesHandlerImpl implements ImagesHandler {
    ....
    public boolean doScreen() throws IOException, AWTException {
        final Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        ...
    }
}

我的应用使用spring boot创建,我需要对其进行测试。但我得到java.awt.HeadlessException

我的测试:

@RunWith(SpringRunner.class) 
@SpringBootTest(classes = ImagesHandlerImpl.class) 
@ContextConfiguration(classes = App.class) 
public class ImagesHandlerImplTest { 
    ...
    @Test
    public void whenDoScreenThenFilenameLikeTemplate() throws IOException, AWTException {
        imagesHandler.doScreen();
        final String name = dir.listFiles()[0].getName();
        assertThat("SCREEN_0", is(name));
    }
}

我尝试阻止HeadlessException:

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplicationBuilder builder = new SpringApplicationBuilder(App.class);
        builder.headless(false).run(args);
    }
}

我使用的是spring-boot版本1.5.6.RELEASE。

但它没有帮助。我得到了日志:

  

ava.awt.HeadlessException at   sun.awt.HeadlessToolkit.getScreenSize(HeadlessToolkit.java:284)at at   org.robinhood.image.ImagesHandlerImpl.doScreen(ImagesHandlerImpl.java:42)     在   org.robinhood.image.ImagesHandlerImplTest.whenDoScreenThenCreatePrintScreen(ImagesHandlerImplTest.java:51)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)at   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.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)at   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)     在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)     在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)at at   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)at at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)at at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)at at   org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)     在   org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)     在   org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)     在   org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)     在org.junit.runners.ParentRunner.run(ParentRunner.java:363)at   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)     在org.junit.runner.JUnitCore.run(JUnitCore.java:137)at   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)

帮帮我。如何解决这个问题? 谢谢!

2 个答案:

答案 0 :(得分:3)

春季测试跑步者不会调用main method的{​​{1}}。 它默认情况下会启用Headless mode

您可以指定JVM参数App class来执行测试用例。 另一个解决方案是在bean初始化期间设置属性。

-Djava.awt.headless=false

答案 1 :(得分:1)

添加

// overcome @SpringBootTest setup
System.setProperty("java.awt.headless", "false")

doScreen()方法调用之前。