尝试在@After方法中使用来自@Test方法的结果图像

时间:2019-05-01 18:16:55

标签: java junit

我编写了一个程序,可以使用随机颜色为图像着色。我正在尝试在JUnit中测试某些方法,并希望保存新图像。问题是我收到一个IllegalArgumentException错误,告诉我图像== null。

一些代码演示:

    private Formatter formatter;
    private BufferedImage newPicture;
    private BufferedImage picture;

@Before
public void start() throws IOException {
    formatter = new Formatter("red");
    picture = ImageIO.read(---some folder ---);
}
@Test
public void colorTest() {
    newPicture = formatter.color(picture);
    assertEquals(newPicture.getHeight(), picture.getHeight());
}

@After
public void save() {
    try {
        ImageIO.write(newPicture, "png", new File("-another folder-");
    } catch (IOException exception) {
        System.out.println("Error");
    }
}

这似乎是@After方法的问题。运行测试后,可以在save()中指定的文件夹中找到newPicture。这是什么问题?

编辑Stacktrace:

java.lang.IllegalArgumentException: image == null!
at java.desktop/javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925)
at java.desktop/javax.imageio.ImageIO.getWriter(ImageIO.java:1608)
at java.desktop/javax.imageio.ImageIO.write(ImageIO.java:1540)
at org.jis.generator.GeneratorTest.save(GeneratorTest.java:122)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
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.RunAfters.evaluate(RunAfters.java:33)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
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.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

最终编辑: 我发现了问题。我有一些其他测试对newPicture没有任何作用,因此@After在newPicture中使用null运行。通过添加一个if语句来解决该问题,该语句检查newPicture是否等于null。

1 个答案:

答案 0 :(得分:0)

您使用Junit 4还是5?我在使用@Before和@After的老式引擎和旧的Junit 4测试用例时遇到了一些麻烦。

如果您将Junit 5与Vintage一起使用,请切换到@BeforeEach和@AfterEach。

如果您仍然使用JUnit 4,则您的代码中看不到任何错误,您可以发布Stacktrace吗?