Question

Is it possible to assing value to mock object. Ex:

myMockObject = context.mock(MyObject.class);

myMockObject.setId("someId");

My method which I'm testing reaches the end, but at the end there is method for validation of that object so object without id is considered to be invalid. Is there anything else I can do about this?

Can I somehow specify ok I'm expecting this exception but pass the test anyway?

I found this link but I'm unable to found solution :

http://www.jmock.org/yoga.html

I'm expecting logger to throw an validation exception with message string, did anyone have experience with this before?

I tried this :

context.checking(new Expectations() {
            {

allowing(logger).error(with(exceptionMessage));

    }
        });

Note exceptionMessage message is thrown by the validation method which validates the object at the end of the method which I'm testing.

Was it helpful?

Solution

This was the answer I was looking for :

http://www.jmock.org/throwing.html

OTHER TIPS

You need to add an Expectation that causes the mock method to return the value you expect:

allowing (myMockObject).getId(); will(returnValue("someId"));

This will cause getId to return the value you expect, and since it is using the allowing invocation count it won't cause the test to fail if it is not called.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top