Check Delay EasyMock

I am using EasyMock to create mock objects for testing JUnit in Java. I create a mock object and pass it to another thread where it expects methods to be called. In another thread, calls are enclosed in a block try/catch(Throwable), so when an unexpected call occurs in the layout and therefore throws AssertionError, this error is captured by the catch block and processed. Thus, despite an unexpected call, the test passes.

In order for the test to fail, I would like to defer all call checks for the call EasyMock.verify(mock)made in the test runner thread at the end. Is this possible and how?

+3
source share
4 answers

The right decision, which I think of, is to stop fishing Throwable. Doing this catches everything Error, how do you find it can be quite dangerous ... are you absolutely sure that you are 100% sure what you need to catch Throwable? Why?

(If this happens, you can catch it on AssertionErrorpurpose and change it, but it's ugly!)

+1
source

I'm not sure how to do this with EasyMock, but this behavior is possible with Mockito , since validation statements can be specified at the end of the test.

+1
source

:

http://easymock.org/EasyMock2_5_2_Documentation.html

" Mock Object, createMock(), AssertionError . "" Mock Object, (0, null false), createNiceMock(). "

, AssertionError, verify() ( AssertionErrors)

0

@deterb, Mockito, . :

:

public interface MyInterface {

    void allowedMethod();

    void disallowedMethod();
}

, AssertionError:

public class UserClass {

    public UserClass() {
    }

    public static void throwableCatcher(final MyInterface myInterface) {
        try {
            myInterface.allowedMethod();
            myInterface.disallowedMethod();
        } catch (final Throwable t) {
            System.out.println("Catched throwable: " + t.getMessage());
        }
    }
}

Mockito:

@Test
public void testMockito() throws Exception {
    final MyInterface myInterface = mock(MyInterface.class);

    UserClass.throwableCatcher(myInterface);

    verify(myInterface, never()).disallowedMethod(); // fails here
}

EasyMock, :

@Test
public void testEasyMock() throws Exception {
    final AtomicBoolean called = new AtomicBoolean();
    final MyInterface myInterface = createMock(MyInterface.class);
    myInterface.allowedMethod();

    myInterface.disallowedMethod();
    final IAnswer<? extends Object> answer = new IAnswer<Object>() {

        @Override
        public Object answer() throws Throwable {
            System.out.println("answer");
            called.set(true);
            throw new AssertionError("should not call");
        }

    };
    expectLastCall().andAnswer(answer).anyTimes();

    replay(myInterface);

    UserClass.throwableCatcher(myInterface);

    verify(myInterface);
    assertFalse("called", called.get()); // fails here
}

, , , myInterface.disallowedMethod() expectLastCall().andAnswer(answer).anyTimes().

Proxy class ( InvocationHandler) . , .

, , EasyMock. :

public class MockedMyInterface implements MyInterface {

    private final MyInterface delegate;

    private final AtomicBoolean called = new AtomicBoolean();

    public MockedMyInterface(final MyInterface delegate) {
        this.delegate = delegate;
    }

    @Override
    public void allowedMethod() {
        delegate.allowedMethod();
    }

    @Override
    public void disallowedMethod() {
        called.set(true);
        throw new AssertionError("should not call");
    }

    public boolean isCalled() {
        return called.get();
    }

}

:

@Test
public void testEasyMockWithCustomClass() throws Exception {
    final MyInterface myInterface = createMock(MyInterface.class);
    myInterface.allowedMethod();

    final MockedMyInterface mockedMyInterface = 
        new MockedMyInterface(myInterface);

    replay(myInterface);

    UserClass.throwableCatcher(mockedMyInterface);

    verify(myInterface);
    assertFalse("called", mockedMyInterface.isCalled()); // fails here
}
0

Source: https://habr.com/ru/post/1727344/


All Articles