I've written a program that colors images with random colors. I'm trying to test certain methods in JUnit and would like to save the new images. The problem is that I'm getting an IllegalArgumentException error telling me that the image == null.

Some code to demonstrate:

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

This seems to be a problem with the @After method. The newPicture is found in the folder specified in save() after I run the test. What is the issue here?

EDIT for 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)

FINAL EDIT: I figured out the issue. I had some other tests that didn't do anything to newPicture, so the @After was run with null for newPicture. Fixed the issue by adding an if-statement that checks if newPicture is equal to null.

1 Answers

0
Community On

Do you use Junit 4 or 5? I have had some troubles with the vintage engine and old Junit 4 test cases with @Before and @After.

If you use Junit 5 with Vintage switch to @BeforeEach an @AfterEach.

If you still use JUnit 4, I see no error in you're code, can you post the Stacktrace?