0

我正在构建一个测试,我需要在其中发送问题,然后等待答案。消息传递不是问题。事实上,为了弄清楚哪个答案对应于哪个问题,我使用了一个 id。我的 id 是使用 UUID 生成的。我想检索这个 id,它是作为模拟对象的参数给出的。它看起来像这样:

oneOf(message).setJMSCorrelationID(with(correlationId));
    inSequence(sequence);

其中correlationId 是我想保留的其他期望的字符串,例如:

   oneOf(session).createBrowser(with(inputChannel), 
           with("JMSType ='pong' AND JMSCorrelationId = '"+correlationId+"'"));

你有答案吗?

4

3 回答 3

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));
    }
}
}
于 2010-05-25T22:23:15.960 回答
0

我在这个网站上找到了另一个解决方案 http://www.symphious.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);
}});
于 2010-05-26T14:11:07.460 回答
0

要考虑的另一种选择是,相关 ID 来自哪里?是否应该注入该活动以便您可以控制它并在测试中检查它?

于 2010-05-28T20:08:49.537 回答