Question

I have a callback interface of ~10 methods that I am using in a unit test. I need to test the value of an XML string that is set during the test.

I am already committed to jmock for other test cases.

It seems that the preferred 'jmock way' to do this is to write a custom org.hamcrest.Matcher object to test the values of the returned string.

However this sounds hard. Is there a simpler way ?

  • Is there an easy way to get a jmock matcher to pass a String value into an enclosing String[]? Then I could examine the string outside the jmock clause
  • Is there an easy way to extend a mock object so I implement my own method. I was hoping for something like
Callback testCB = mockery.mock(Callback.class)
{
    public void myOverrideMethod(String strIn)
    {
        ... I can examine the string values in here ...
    }
};

I could define my own stub object of course, but I'm lazy...

Was it helpful?

Solution

If you know enough to code the solution, you should be able to cope with writing a matcher? Try extending TypeSafeDiagnosingMatcher and see the two methods you're asked to fill in. The important thing is to watch the test fail and make sure that the diagnostic messages are helpful.

As you get used to this, you should find that you start to build up a little collection of matchers that you can reuse.

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