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;

    public static void setUpBeforeClass() throws Exception {


    public static void tearDownAfterClass() throws Exception {

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

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

    public void testSetBonusPoints() {
        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;

6 Years
Discussion Span
Last Post by peter_budo

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.