0

I have a method set up to add users to an ArrayList, and return false if the same user is added:

public boolean addUser(K pass, V user)
{
    if (username.contains(user))
    {
        return false;
    }
    else
    {
        password.add(pass);
        username.add(user);
        size++;
        return true;
    }
}

Then I have a JUnit test to assertFalse(true) because that's what my method returns when adding two users with the same name:

@Test
public void testAddingTwoOfTheSameUserToArrayList() {
    User<String, String> user1 = new User<String, String>();
    user1.addUser("password", "SpeedinDave");
    user1.addUser("password", "SpeedinDave");
    assertFalse(true);
}

However, it the test always turns up false (red bar). Any ideas?

5
  • what's username object? Commented Jun 4, 2013 at 15:45
  • 2
    What do you expect assertFalse(true) to do? And why are you never using the results of addUser? Commented Jun 4, 2013 at 15:46
  • 1
    you are wanting to assert the return value, so you need to capture the return value and pass that as the argument to assertFalse() Commented Jun 4, 2013 at 15:47
  • Your current "test" isn't actually testing your method. Commented Jun 4, 2013 at 15:48
  • First, what @Quetzalcoatl said. Second, if you do use assertFalse you should put in false NOT true. Commented Jun 4, 2013 at 16:02

2 Answers 2

8

You should test something like that:

@Test
    public void testAddingTwoOfTheSameUserToArrayList() {
        User<String, String> user1 = new User<String, String>();
        user1.addUser("password", "SpeedinDave");
        assertFalse(user1.addUser("password", "SpeedinDave"););
    }

to be sure that the second addUser() returns false.

assertFalse(b) will succeed if the parameter b is false. So if you pass true, it will always fail.

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

1 Comment

It might also be worthwhile to modify the first user1.addUser("password", "SpeedinDave"); to be assertTrue(user1.addUser("password", "SpeedinDave"));
3

Right now you are not testing the return value of your addUser() method, but rather that the boolean constant true evaluates to false (this will never be true). You probably want something like:

@Test
    public void testAddingTwoOfTheSameUserToArrayList() {
        User<String, String> user1 = new User<String, String>();
        assertTrue(user1.addUser("password", "SpeedinDave"));
        assertFalse(user1.addUser("password", "SpeedinDave"));
    }

Comments

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.