4

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.

user590444
  • 4,252
  • 8
  • 36
  • 43
  • Can you share the code that produces this error? – Mureinik Dec 25 '13 at 07:09
  • 1
    Please show your code. `"1" != 1` and sometimes `"1" != "1"`. – Elliott Frisch Dec 25 '13 at 07:09
  • 2
    Please verify that the agruments you are specifying in the mock are same as the call to the mock. There would be one call to the mock but it may not match the exact specification you have provided to the expect call. – udit bansal Dec 25 '13 at 13:15
  • My test used multithreading classes which caused that error. See http://stackoverflow.com/questions/12159/how-should-i-unit-test-threaded-code – user590444 Dec 27 '13 at 12:38
  • @uditbansal is probably right. But a sample of the code and the full exception you receive would be helpful to confirm that :) Also, if you're expecting null, don't forget to use `EasyMock.isNull(MyClass.class)` in the expectation. You could receive a null Object (not a null MyClass) and it would throw this type of error. – Dan Temple Dec 30 '13 at 16:34
  • EasyMock.makeThreadSafe may help in my case – user590444 Jan 13 '14 at 11:47

4 Answers4

4

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.

Michal
  • 41
  • 2
2

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"

Z Yang
  • 21
  • 1
0

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);
piotrek
  • 13,982
  • 13
  • 79
  • 165
0

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.

hcpl
  • 17,382
  • 7
  • 72
  • 73