2

Okay, here's what happening. I am attempting to set up test code for a Library class that I set up. Now, I'm supposed to adjust my constructor to automatically create an entry in my array list before a new one is added. Here's my constructor:

public DvrLibrary()
{
    recordings = new ArrayList < DvrRecording > ();

    DvrRecording initialRecording = new DvrRecording();
    initialRecording.setTitle("Family Guy");
    initialRecording.setStartTime("10:00pm");
    for (int i = 0; i < 3; ++i) { initialRecording.increaseTime(); }

    recordings.add(initialRecording);
}

When I created my test case, I have the test code as:

public void testARecording()
{
    DvrLibrary testLibrary = new DvrLibrary();
    ArrayList < DvrRecording > testList = new ArrayList < DvrRecording > ();

    DvrRecording testRecording = new DvrRecording();
    testRecording.setTitle("Lost");
    testRecording.setStartTime("Later");
    for (int i = 0; i < 3; ++i) { testRecording.increaseTime(); }

    testLibrary.addRecording(testRecording);
    testList.add(testRecording);       

    assertEquals(testLibrary.getRecordings(), testList);
}

My problem is, when I run the actual test, the default, I'm pretty sure, is not being called. It is returning a failed error message telling me that it was expecting to find two entries but only found one. I tried cutting out the new entry that the test code is creating and the error read that there was supposed to be one, but found nothing.

This leads me to believe that there is something wrong with the way I set up the constructor in the actual code, but I cannot for the life of me figure out what I messed up. When I went to get help, the guy I talked to was like welllll I'm not sure what's wrong but you could be messing up the assert. But I don't know what's wrong?

Anyone have any input? I'd really appreciate it. Thanks!

2
  • Post exact error message you are getting. Commented Mar 1, 2012 at 21:29
  • 1
    i'm a little confused. if you add "family guy" in your testLibrary, and you add "lost" in the testList, why would you expect the two lists to be the same? Commented Mar 1, 2012 at 21:30

3 Answers 3

2

Since assertEquals signature is (expected, actual), your error message is inverted. It should be "Expecting one, got two".

Your actual list is correct, it has two items, as directed by the code. The expected one, however, is incorrect: it has only a single item that you added to it during the test set-up.

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

Comments

2

If I follow you, JUnit outputs that it expected two entries and found one. So, testLibrary.getRecordings() has two entries and testList has one.

Isn't this expected? testLibrary has one entry from the constructor, and one you add in the test code. testList has one entry, the one you add in your test code.

Comments

1

assertEquals expects the "expected" argument first, and the "tested" argument second. This may just be a vanilla test failure, except you're mixing up the arguments.

5 Comments

assuming junit...testng's assert methods do tested first then expected. ;)
ummm...true. you're assuming junit. testng does it the other way around : testng.org/javadocs/org/testng/Assert.html
Right. I see. I hadn't heard of testng, and assumed you had made a typo in the word "testing." My apologies.
no worries. i realized that everyone was assuming s/he's using junit, so i'm probably the weirdo here.

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.