2

it's my first time using JUNIT and I literally cannot get it to work.

I've got a person class, in which has firstName and lastName, and a test class in which I need to test the methods. Everytime I attempt to test one though, if i've wrote a test for the particular method, it fails.

Here is my code.

Person Class

public class Person {
    private String firstName;
    private String lastName;    

    public Person (String a, String b) {
        firstName = a;
        lastName = b;
    }

    public String getfirstName() {
        return firstName;
    }

    public void setfirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getlastName() {
        return lastName;
    }

    public void setlastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return  firstName + " " + lastName;
    }
}

Person Test class

public class PersonTest {
    Person p1;

    public PersonTest() {
        Person p1 = new Person ("Thomas", "Brown");
    }

    @Before
    public void setUp() {}

    @After
    public void tearDown() {}

    @Test
    public void testGetfirstName() {
        assertEquals("Thomas", p1.getfirstName());
    }

    @Test
    public void testSetfirstName() {}

    @Test
    public void testGetlastName() {
        assertEquals("Brown", p1.getlastName());
    }

    @Test
    public void testSetlastName() {}

    @Test
    public void testToString() {
        assertEquals("Thomas Brown", p1.toString());
    }
}

Could anyone point me in the right direction?

1
  • 2
    Move the creation of Person p1... to your @Before method. Commented Nov 14, 2013 at 13:20

2 Answers 2

6

This is the right way to do it:

@Before
public void setUp() {
    p1 = new Person ("Thomas", "Brown");
}

You have 2 problems in your code.

public PersonTest() {
    Person p1 = new Person ("Thomas", "Brown");
}

This creates a local variable and your field p1 stays null.

The second is that in your setUp method you do not initialize p1.

The method you annotate with @Before will run before every test to you should put the initialization there. I also suggest to use more descriptive names so you should change p1 to target or something like that.

Edit: For your set... methods you can do something like this:

public class PersonTest {
    private static final String TEST_FIRST_NAME = "some name";

    Person target;

    // ...
    @Test
    public void testSetFirstName() {
        target.setFirstName(TEST_FIRST_NAME);
        Assert.assertEquals(target.getFirstName(), TEST_FIRST_NAME);
    }
}

At that point you can assume that getFirstName works since you have a test for it as well.

A sidenote: I think you don't have to test getters and setters as long as you generate them with Eclipse. It is just unnesessary.

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

5 Comments

Excellent thanks. I'm also confused about how I go about testing set methods? Could you point me in the right direction with that?
You may want to test your accessors/mutators if your production class is asserting inputs and/or valid state.
That's the job of a validator class or something like that, no?
True, but not all systems make use of external validator classes for simple things like nulls and empty strings.
It might be an overkill but on a previous system I was working on we created a NonNullParametrized aspect which checked all methods in the business facade and threw an exception if the user supplied null. In this case we only had to test the aspect not all methods. At the end of the day it was much less work for the same result.
3

Change

Person p1 = new Person ("Thomas", "Brown"); //this is local variable

to

 p1 = new Person ("Thomas", "Brown");// and this will use the instance variable

2 Comments

While it is correct to remove this aliasing, it is usually bad form to initialize the class under test in a test constructor (where it is only created once) instead of in a @Before method (where it is initialized for each @Test). If you use the constructor method and you modify the class under test at any point, you're introducing side-effects for all of your @Test methods.
Good note, actually I am not familiar to JUnit, I just knew the reason of NullPointerException :) But thanks for the good information, hope it'll be useful for him.

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.