我正在建立一个测试,我需要发送问题,然后等待答案。 消息传递不是问题。事实上要弄清楚哪个答案对应于哪个问题,我使用id。我的id是使用UUID生成的。我希望检索此id,它作为模拟对象的参数给出。 它看起来像这样:
oneOf(message).setJMSCorrelationID(with(correlationId));
inSequence(sequence);
其中correlationId是我想要保留的字符串,如下所示:
oneOf(session).createBrowser(with(inputChannel),
with("JMSType ='pong' AND JMSCorrelationId = '"+correlationId+"'"));
你有答案吗?
答案 0 :(得分:4)
你必须创建自己的行动。这是我的:
/**
* puts the parameter array as elements in the list
* @param parameters A mutable list, will be cleared when the Action is invoked.
*/
public static Action captureParameters(final List<Object> parameters) {
return new CustomAction("captures parameters") {
public Object invoke(Invocation invocation) throws Throwable {
parameters.clear();
parameters.addAll(Arrays.asList(invocation.getParametersAsArray()));
return null;
}
};
}
然后你就像这样使用它(使用静态导入):
final List<Object> parameters = new ArrayList<Object>();
final SomeInterface services = context.mock(SomeInterface.class);
context.checking(new Expectations() {{
oneOf(services).createNew(with(6420), with(aNonNull(TransactionAttributes.class)));
will(doAll(captureParameters(parameters), returnValue(true)));
}});
要做你想做的事,你必须实现自己的匹配器。这就是我被破解的东西(遗漏了一些空的检查,当然我只是使用了众所周知的样本接口):
@RunWith(JMock.class)
public class Scrap {
private Mockery context = new JUnit4Mockery();
@Test
public void testCaptureParameters() throws Exception {
final CharSequence mock = context.mock(CharSequence.class);
final ResultSet rs = context.mock(ResultSet.class);
final List<Object> parameters = new ArrayList<Object>();
context.checking(new Expectations(){{
oneOf(mock).charAt(10);
will(doAll(JMockActions.captureParameters(parameters), returnValue((char) 0)));
oneOf(rs).getInt(with(new ParameterMatcher<Integer>(parameters, 0)));
}});
mock.charAt(10);
rs.getInt(10);
}
private static class ParameterMatcher<T> extends BaseMatcher<T> {
private List<?> parameters;
private int index;
private ParameterMatcher(List<?> parameters, int index) {
this.parameters = parameters;
this.index = index;
}
public boolean matches(Object item) {
return item.equals(parameters.get(index));
}
public void describeTo(Description description) {
description.appendValue(parameters.get(index));
}
}
}
答案 1 :(得分:0)
我在这个网站上找到了另一个解决方案 http://www.symphonious.net/2010/03/09/returning-parameters-in-jmock-2/
import org.hamcrest.*;
import org.jmock.api.*;
public class CapturingMatcher<T> extends BaseMatcher<T> implements Action {
public T captured;
public boolean matches(Object o) {
try {
captured = (T)o;
return true;
} catch (ClassCastException e) {
return false;
}
}
public void describeTo(Description description) {
description.appendText("captured value ");
description.appendValue(captured);
}
public Object invoke(Invocation invocation) throws Throwable {
return captured;
}
}
然后可以像:
一样使用它 context.checking(new Expectations() {{
CapturingMatcher<String> returnCapturedValue = new CapturingMatcher<String>();
allowing(mockObject).getParameter(with(equal("expectedParameterName")), with(returnCapturedValue)); will(returnCapturedValue);
}});
答案 2 :(得分:0)
还需要考虑另一个选项,相关ID来自何处?是否应该注入该活动以便您可以控制它并在测试中检查它?