user590444
user590444

Reputation: 4322

EasyMock unclear error: "expected: 1, actual: 1"

I got fail of test with

java.lang.AssertionError: 
  Expectation failure on verify:
    A.logon(null): expected: 1, actual: 1
    at org.easymock.internal.MocksControl.verify(MocksControl.java:226)
    at org.easymock.EasyMock.verify(EasyMock.java:2080)
    at com.ATest.test

What this error means? Why it may happen? Please suggest.

Upvotes: 4

Views: 4669

Answers (4)

Z Yang
Z Yang

Reputation: 21

Here's the potential solution:

  1. If your code is multithreading, try How should I unit test threaded code?

  2. If you're using createStrictMock, you may want to make sure the method calls are in the correct order, otherwise it could return things like "expected: 1, actual: 1" or "expected: at least 0, actual: 1"

Upvotes: 2

Hans Cappelle
Hans Cappelle

Reputation: 17495

More code needed

We will need more code for a definitive answer so until that happens this is my best guess.

Create a mock of your class

You have class A mocked somewhere in your test using PowerMock or EasyMock, something like:

A mockedA = EasyMock.createMock(A.class);

Expect behaviour on mocked class

So all behaviour upon mocked class A that happens during your specific test (again missing code here) should be expected with:

final String someValue = "someValue";
EasyMock.expect(mockedA.logon(EasyMock.isA(String.class))).andReturn(someValue)

I'm totally guessing here on the signature of that logon method since I don't know your A class implementation. In my guess the logon method expects a String argument (can by any class or primitive really) and returns another String value.

Matching

If you're matching for another argument, like argument of class B for example you need to update the above code to:

EasyMock.expect(mockedA.logon(EasyMock.isA(B.class))).andReturn(someValue)

Same can be said for the type of the return argument, if that is of type C just instance a C class object for someValue last param instead.

Unclear errors

You have a valid point that the errors are very unclear in some cases. You are matching for something that accepted an argument object that was null in your test.

Now null can be matched in several ways, like just with fixed null value or with EasyMock.isA(class). And that is likely where it went wrong in your test some experiment with the following:

EasyMock.expect(mockedA.logon(null)).andReturn(someValue)
EasyMock.expect(mockedA.logon(EasyMock.isA(SomeClass.class)).andReturn(someValue)
EasyMock.expect(mockedA.logon(EasyMock.isNull(SomeClass.class)).andReturn(someValue)
EasyMock.expect(mockedA.logon(aVariableHoldingNull)).andReturn(someValue)
EasyMock.expect(mockedA.logon(EasyMock.eq(aVariableHoldingNull)).andReturn(someValue)
EasyMock.expect(mockedA.logon(EasyMock.anyObject(SomeClass.class)).andReturn(someValue)
EasyMock.expect(mockedA.logon(EasyMock.isNull()).andReturn(someValue)

And this list is far from complete. Also make sure to get back to us with your solution so the community can learn from this.

Upvotes: 0

Michal
Michal

Reputation: 41

I had similar error. It turned out that the method was called on different thread. Making the method executing in the same thread solved the problem.

Upvotes: 4

piotrek
piotrek

Reputation: 14530

what you see is just a result of toString() method called on both arguments. so in fact you may be doing any combination of:

assertEquals(1L, 1);
assertEquals("1", 1);
assertEquals('1', 1);
assertEquals(customObject, 1);

Upvotes: 0

Related Questions