Reputation: 4322
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
Reputation: 21
Here's the potential solution:
If your code is multithreading, try How should I unit test threaded code?
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
Reputation: 17495
We will need more code for a definitive answer so until that happens this is my best guess.
You have class A
mocked somewhere in your test using PowerMock or EasyMock, something like:
A mockedA = EasyMock.createMock(A.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.
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.
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
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
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