2

I'm trying to use setUp to initialise an array of objects in JUnit for my test methods but I'm doing something wrong as the tests throw up errors (null pointer exception). They run fine when I initialise arrays in the test methods themselves but this obviously isn't ideal. Can anyone point out what I'm doing wrong here?

class MainTest {

    Lord baratheons[];
    Lord starks[];

    //Setup & Teardown

    @Before
    static void setUp() throws Exception {
        Lord baratheons[] = new Lord[3];
        baratheons[0] = new Lord("Robert", 15);
        baratheons[1] = new Lord("Renly", -5);
        baratheons[2] = new Lord("Stannis", 30);
        System.out.println("Baratheons initialised!");

        Lord starks[] = new Lord[3];
        starks[0] = new Lord("Robb", -60);
        starks[1] = new Lord("Eddard", 0);
        starks[2] = new Lord("Jon", 90);
        System.out.println("Starks initialised!");
    }

    //Tests

    @Test
    void testGratefulLord() {
//      Lord baratheons[] = new Lord[3];
//      baratheons[0] = new Lord("Robert", 15);

        int x = baratheons[0].getRelationship();
        baratheons[0].giveFief();
        assertEquals(baratheons[0].getRelationship(), (x+=10));

    }

EDIT:

N.B In addition to following the steps outlined in the below solutions, I'd like to note for posterity that I was also using the wrong tag for the setup. As this is JUnit 5, the tag is @BeforeEach. @Before is the tag for JUnit 4, which was why the setup method wasn't being called. I hope this is helpful to future users.

8
  • 3
    This is a scope issue. You're declaring an array inside the setUp() with the same name as the global array. Just do baratheons = new Lord[3]; and it should work Commented Apr 25, 2018 at 15:06
  • Also, for good coding style, you should put the array brackets after the class, not the variable name. So use Lord[] baratheons instead Commented Apr 25, 2018 at 15:09
  • As in just declare it inside the setUp()? This prevents the int x = baratheons[0].getRelationship(); to fail to resolve baratheon to a variable. Commented Apr 25, 2018 at 15:09
  • It shouldn't fail to resolve unless you removed the global declaration. Leave that and just change the lines to baratheons = new Lord[3]; and starks = new Lord[3]; inside setUp() Commented Apr 25, 2018 at 15:13
  • 1
    Then remove static from setUp method. Commented Apr 25, 2018 at 15:17

4 Answers 4

3

The issue here is that you are re-declaring the arrays inside of your setUp() method. This is messing with the Scope of the objects you want to use.

Remove the static from the the setUp() method as that is not needed.

Change your code from

Lord baratheons[] = new Lord[3];
Lord starks[] = new Lord[3];

to

baratheons = new Lord[3];
starks = new Lord[3];

Lastly, you need to change your methods to be public. Why? Because JUnit uses reflection behind the scenes and they need to be public for it to recognize. You can view the JUnit JavaDoc and see that is explicitly mentions a public void method

Sign up to request clarification or add additional context in comments.

4 Comments

I followed your steps and I'm still getting a null pointer exception. I've declared Lord[] baratheons; outside of setUp() and baratheons = new Lord[3]; inside setUp(). Ditto for starks
Only other difference I see between your code and what I run is that I have public in front of all my methods and declare the global variables as private - but I don't see why that would affect anything. Let me look more into it. Can you post the important part of the stack trace?
What version of JUnit are you using btw?
I was wrong, make the methods public - added to my answer
1

Remove static from setUp method.

Also

Lord baratheons[] = new Lord[3];

should just be

baratheons = new Lord[3];

Same goes for the starks.

1 Comment

baratheons[] in the "should be" is incorrect. There should be no brackets there
0

The setUp() should not be static.

The arrays you instantiate in setUp() method are assigned to local variables.

Lord baratheons[] = new Lord[3];

The variable you declare here is shadowing your class attribute. You should just remove the class here (Lord) to assign your new array to the good variable.

baratheons = new Lord[3];

Also be careful with your assert statement:

assertEquals(baratheons[0].getRelationship(), (x+=10));

This statement is working but using '+=' syntax here may induce errors. In this case, the code is pretty simple but you'll gain in clarity if you extract the statement 'x+=10' if you want to change the value of x. If you don't need to change the value, you may use x+10 in place.

Comments

0

The issue here is with scope of your variables baratheons and starks.

Have a read of the Java Specification on the Scope of a Declaration to better understand the issue.

Hope this helps you understand the issue a little more.

Your code should be as follows:

class MainTest {

    private Lord[] baratheons;
    private Lord[] starks;

    //Setup & Teardown

    @Before
    public void setUp() throws Exception {
        baratheons= new Lord[3];
        baratheons[0] = new Lord("Robert", 15);
        baratheons[1] = new Lord("Renly", -5);
        baratheons[2] = new Lord("Stannis", 30);
        System.out.println("Baratheons initialised!");

        starks = new Lord[3];
        starks[0] = new Lord("Robb", -60);
        starks[1] = new Lord("Eddard", 0);
        starks[2] = new Lord("Jon", 90);
        System.out.println("Starks initialised!");
    }

    @Test
    public void testGratefulLord() {
        int x = baratheons[0].getRelationship();
        baratheons[0].giveFief();
        assertEquals(baratheons[0].getRelationship(), (x += 10));
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.