views:

72

answers:

2

I'm using jmock to mock out an OutputStream and set expectations on the data that gets written to it.

So I have an expectation that looks something like this

oneOf(stream).write(byteArrayMatching("Some string"));

(byteArrayMatching) is a factory for a custom matcher.

This all works fine, except when the test fails because the class under test writes incorrect data, I get an error looking something like this:

java.lang.AssertionError: unexpected invocation: stream.write([<60>, <63>, ...])

It's pretty hard to identify what exactly is wrong with the data by looking at the sequence of bytes (I haven't gotten around to memorizing ASCII yet). This pretty much forces me to run the test in a debugger to figure out what's wrong.

My question is: is there a way to somehow register a formatter of sorts with the mock object or the Mockery object which can pretty print a parameter value? It's clear that jmock is already doing some pretty-printing, since the above is not the output of byte[].toString(), but I can't find anything in the API docs that suggests a way to customize this pretty-printing logic

A: 

There is no way to currently do this in the current (2.5.1) jMock library. I would suggest you log an enhancement to jMock.

shek
A: 

The cute answer is that mocking makes more sense against a type that you've defined, that has some domain structure to it, rather than external API.

The next answer is to look at the new version of Hamcrest which includes support for reporting a mismatch.

The next answer, unless there's sequence involved, is that in this case it might be better to use an in-memory byte stream and assert the string afterwards.

And file an issue too, please :)

Steve Freeman