views:

31

answers:

2

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.

+1  A: 

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.

codelark
@codelark where will I write this code? Inside `context.checking(new Expectations() {` or ? I'm getting compile error, can't find stubs `The method stubs() is undefined for the type myMockObject` +1 for the effort
c0mrade
@c0mrade what version of jMock are you using?
codelark
@codelark I'm using 2.5.1
c0mrade
@c0mrade edited to reflect the correct version of jMock, see above.
codelark
@codelark this validate method doesn't call the getter method that is the issue, but it does send the validation error to the logger so I though I could expect logger to send certain message, but I guess version doesn't support with(eq(errorString)) as I saw in the link I posted in my edit
c0mrade
+2  A: 

This was the answer I was looking for :

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

c0mrade