assertEquals, what is actual and what is expected?

39,560

Solution 1

Most testing frameworks (the xUnit family) are based on the JUnit framework. The Assert family of functions in JUnit have the (expected, actual) format; it became a convention, and most of the other frameworks followed that convention.

Some frameworks (like TestNG or NUnit 2.4+ for .NET) reversed that order (with the use of a constraint-based model for NUnit) to increase readability ("make sure that the actual value is 56" feels more natural than "make sure that 56 is the actual value").

The bottom line is: stick to the convention of your framework. If you use JUnit, put the expected value first. If you use TestNG, put the actual value first. You're right, that makes no difference in the test results when you accidentally reverse the arguments. But it makes a big difference in the default message you get from a failing test. When your reversed assertEquals(ShouldBeTrueButReturnsFalse(), true) in JUnit fails, the default message says "expected [false] but found [true]", where it should have said "expected [true] but found [false]". This is confusing, to say the least, and you shouldn't have to deal with a possible misdirection of that message.

Some of the unit tests in the Github link you provide don't follow the convention and have the same problem. Don't do that. Stick to the convention of your framework.

Solution 2

Answer is simple. JUnit has reverse order of arguments. Refer the example below:

JUnit:

void assertEquals(Object expected, Object actual)

TestNG:

void assertEquals(int actual, int expected)

Solution 3

I too had the same confusion.

But the confusion is because the Github search returns the assertEquals syntax for both TestNG and junit. You are correct with your expected & actual understanding.

TestNG: assertEquals(Object actual, Object expected)

junit: assertEquals(Object expected, Object actual)

Eg., in testNG result of below code

int x=1+2; assertEquals(x,2);

is:

java.lang.AssertionError: expected [2] but found [3]
Expected :2
Actual   :3
Share:
39,560

Related videos on Youtube

insumity
Author by

insumity

Updated on September 06, 2020

Comments

  • insumity
    insumity over 3 years

    I always wondered what exactly is the meaning of actual and expected in assertEquals in libraries like TestNG.

    If we read the Java Docs we see:

    public static void assertEquals(... actual, ... expected)
    Parameters:
        actual - the actual value
        expected - the expected value
    

    From my understanding the expected value is the known one, so the one we expect, and the actual one is the one we want to verify. For example, assume we want to test a function fooBar that always has to return 56.

    In such a case I would do: assertEquals(sth.fooBar(), 56). But with a quick search on GitHub it seems people do it the other way around, so assertEquals(56, sth.fooBar()). But how can the expected value be sth.fooBar() when we don't even know that value? It seems that sth.fooBar() is the actual value which we compare against the expected which we already know.

    I know there is no difference of the correctness of a test but I would like to follow the "correct" way.

    • ControlAltDel
      ControlAltDel over 9 years
      Probably they just did it in a rush and didn't care about the naming order as much as you :)
  • insumity
    insumity over 9 years
    I didn't know that but it actually doesn't answer my question. (so I removed JUnit from my question)
  • talex
    talex over 9 years
    The answer still the same people learn how to use JUnit and after switch to TestNG use same patterns. And vice versa. But this is not the place to ask such question, Because there is no real answer, only opinions.
  • Rahul Sharma
    Rahul Sharma about 7 years
    The question is to explain right way of using expected and actual in testing framework.
  • HASSAN MD TAREQ
    HASSAN MD TAREQ about 5 years
    I was using (expected, actual) for TestNG (coming from xUnit). Now I have to change all the assertEquals(). I would say reversing the order was a bad idea for TestNG :( and Java does not allow named argument (one of the reasons I prefer C#)