SO I have a kinda convoluted problem I will describe it with similar examples instead of copying TONS of code in. The problem comes in that I don't really know how to use JMockit well, dare I say properly.
I have an interface:
public interface IRsrcResolver {
RsrcSet getForQuery(Query, RsrcContext, int);
}
There is a real implementation of it - it is kinda intense. So I made a mocked version that just returns null. I want to then set an [nonstrict]expectation that will return the proper values. It changes test to test.
In code:
public class MockRsrcResolver implements IRsrcResolver {
RsrcSet getForQuery(Query, RsrcContext, int) { return null; }
}
in the Test class I implement it and then want set the expectations (StateEngine uses the RsrcResolver)
public class StateEngineTest {
@Mocked
public IRsrcResolver rsrcResolver;
@Mocked
public RsrcSet rsrcSet;
@Mocked
private StateEngine stateEngine;
public void setUp () throws Exception {
stateEngine = new StateEngine ();
rsrcResolver = new MockRsrcResolver ();
rsrcSet = new RsrcSet ();
new NonStrictExpectations () {
{
rsrcResolver.getForQuery((Query) any, (RsrcContext)any, anyInt);
result = rsrcSet;
}
}
}
@Test
public void testSetState () {
/*... does something that calls getForQuery (...) on an IRsrcResolver ...*/
stateTest.doSomething ();
/* ... assert assert assert ... */
}
}
In doSomething there is something like:
IRsrcResolver m_rsrcResolver = new RsrcResolver ()
RsrcSet m_rsrcSet = m_rsrcResolver.getQuery (query, rsrcCtx, 4);
m_rsrcSet.doSomethingElse ();
m_rsrcSet gets a null value and the debug shows it calling into the null MockRsrcResolver, instead of using the expectation... Am I incorrect in doing it this way? Thanks for any advice you may have!