0

I have the following test code for a class:

/**
 * Test Class
 */
public class GeneratorTestTest {
    private Generator generator;
    private static BufferedImage testImage;
    private BufferedImage rotatedImageTestResult;

    @Before
    public void setUp() throws Exception {
        generator = new Generator(null, 0);

        File inputFile = new File("src/test/resource/picture.jpg");

        testImage = ImageIO.read(inputFile);
    }

    /**
     * Test method
     */
    @Test
    public void testRotateImageRotateImage() {
        //Line 39
        rotatedImageTestResult = generator.rotateImage(testImage, 0);
        assertTrue(imageEquals(testImage, rotatedImageTestResult));
    }

I am get a NullPointerException on the Test method when I try to run it. How can I pass the Junit test?

Here is a full log of the Exception:

line 39 is "rotatedImageTestResult = generator.rotateImage(testImage, 0);

  • 1
    You also need to include the stack trace from the Exception, so that we can see *where* the exception is happening. – tgdavies May 01 '18 at 23:44
  • If you can include the stack trace usually it will include multiple files that caused the failure. Since the nullpointer exception is probably happening in the "rotateImage" method we'd probably need the line numbers that mention the Generator file. – CSLearner May 01 '18 at 23:56
  • your setUp probably hasn't run yet. You need to have it marked as static. – Mrunal Gosar May 02 '18 at 05:52
  • 1
    setUp should not be static: http://junit.sourceforge.net/javadoc/org/junit/Before.html – Ray Tayek May 02 '18 at 06:27

1 Answers1

1
rotatedImageTestResult = generator.rotateImage(testImage, 0);

On this line of code either "generator" is null or some helper object in your generator is null so the "rotateImage" method itself is throwing the null pointer exception.

CSLearner
  • 249
  • 1
  • 5
  • 17