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)));
    }});

やりたいことを行うには、独自のマッチャーを実装する必要があります。これは私がハッキングしたものです (一部の null チェックは省略されています。もちろん、サンプルにはよく知られているインターフェイスを使用しています)。

 @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

考慮すべきもう 1 つのオプションとして、相関 ID はどこから取得されるのでしょうか? アクティビティを制御してテストで確認できるように、そのアクティビティを注入する必要がありますか?

于 2010-05-28T20:08:49.537 に答える
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);
}});
于 2010-05-26T14:11:07.460 に答える