Sky
Sky

Reputation: 301

Junit failed tests, how to fix this?

So I'm tesing using Junit, quite new to it. I am trying to test methods in a class called SetOfUsers as follows:

 @Test
    public void testFindUserByName() {
        System.out.println("findUserByName");
        String name = "";
        SetOfUsers instance = new SetOfUsers();
        User expResult = null;
        User result = instance.findUserByName(name);
        assertEquals(expResult, result);
        // TODO review the generated test code and remove the default call to fail.

    }

So I wanted to check the name of a user entered in Bob for instance in the name string like this

String name = "Bob";

since I have a user called Bob in the setOfUsers class.
The output window displays this message

Failed: expected:<null> but was:<Staff name:Bob, Staff pass:abc123>

What can I do to make this a pass?

Upvotes: 1

Views: 3189

Answers (4)

MariuszS
MariuszS

Reputation: 31567

Read about BDD, this is very nice technique for making tests easy to write and understand (read)

Test-driven development is a software development methodology which essentially states that for each unit of software, a software developer must:

  • define a test set for the unit first;
  • then implement the unit;
  • finally verify that the implementation of the unit makes the tests succeed.

Well written test should have GivenWhenThen sections

(Given) some context

(When) some action is carried out

(Then) a particular set of observable consequences should obtain

This style is known as SpecificationByExample

Given-When-Then is a style of representing tests - or as its advocates would say - specifying a system's behavior using SpecificationByExample.

Example test

@Test
public void testFindUserByName() {

    // given
    SetOfUsers instance = new SetOfUsers();

    // when
    User result = instance.findUserByName("Bob");

    // then
    assertEquals("Bob", result.getName());

}

Nice to read:

Upvotes: 2

gasparms
gasparms

Reputation: 3354

I don't understand the question, but if you want to search for "Bob" why you initialize name=""? The test should be:

@Test
public void testFindUserByName() {
    //Create SetOfUsers
    //Add new User with name Bob
    //FindByUsername("Bob")
    //AssertEqual(User.getName(), "Bob")
}

Upvotes: 0

mdewitt
mdewitt

Reputation: 2534

This test is always going to fail because the last line is

fail("The test case is a prototype.");

The reason your test is failing now is because of the line above,

assertEquals(expResult, result);

You are setting your expected result to null and the result you are getting from the name, "", is probably an empty String as well from that error message. You need to have expResult to be the same as what you expect instance.findUserByName("Bob") to return. However, unless you initialize the instance to be set with a User Object the objects will not match, so it might be better to either mock it to return a pre-created User object so they match, or create a User object with the same properties as the one you expect to be returned check the fields of the User Object returned and the User object you created to be sure they match.

If you want to check for what the user for Bob is, change the code to this:

@Test
public void testFindUserByName() {
    System.out.println("findUserByName");
    String name = "Bob";
    SetOfUsers instance = new SetOfUsers();
    User expResult = <Create an object you expect instance.findUserByName("Bob") to return>;
    User result = instance.findUserByName(name);
    //Check fields here.
    assertEquals(expResult.getUserName(), result,getUserName());
    // TODO review the generated test code and remove the default call to fail.

}

Upvotes: 1

Bohemian
Bohemian

Reputation: 425033

You can't test for null using assertEquals().
To test for null, use:

assertNull(result);

Upvotes: 0

Related Questions