0

I am using JUnit for the first time and I have small problem: the setUp() method doesn't seem to work for me.

public class ClientTest {
    private Client a;


    @BeforeClass
    public static void setUpBeforeClass() throws Exception {

    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Before
    public void setUp() throws Exception {
        Client a = new Client();
    }

    @After
    public void tearDown() throws Exception {
        Client a = null;        
    }

    @Test
    public void testSetBonusPoints() {
        a.setBonusPoints(30);
        assertTrue(30 == a.getBonusPoints());

    }

}

If I initialize the Client a in setUp(), then I receive a NullPointerException. If I initialize it at declaration everything is working fine.

Here is the class Client:

public class Client {
    private int bonusPoints;

    public void setBonusPoints(int bonusPoints) throws IllegalArgumentException {
        this.bonusPoints = bonusPoints;
    }


    public int getBonusPoints() {
        return bonusPoints;
    }

}
3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by peter_budo
1

Java basics!
Class variable Client a will never get initialized because you cretae another temporary variable in setUp method. Just do a = new Client() same goes for tearDown

Edited by peter_budo: typo

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.