dimanche 6 décembre 2020

juint throwing a NullPointerException

I have been scouring the internet for solutions to this. It's my first time implementing junit and I'm fairly new to java (more familiar with Python). I am writing a test class for a Library class and trying to instanciate a Library object using @Before, but I keep getting a null pointer exception for every test method I write. Here is the test class:

class LibraryTest {

    public Library l1;

    @Before
    public void setUp() {
        l1 = new Library("Public Library", "Glasgow");

    }

    @After
    public void tearDown() {
        l1 = null;
    }

    // test methods...

}

Apologies for the formatting, I can't figure out how to tell this editor where my code begins and ends. Hopefully it's clear enough.

An example of a test that fails:

@Test
public void newMembershipNoTest() {
    int before = l1.newMembershipNo();
    int after = l1.newMembershipNo();
    assertEquals(1, after - before);
}

And the method it is testing:

public int newMembershipNo()
{
    highestMembershipNo ++;
    return highestMembershipNo;
}

Aucun commentaire:

Enregistrer un commentaire