Vous devez créer vos propres actions. Voici le mien:
/**
* 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;
}
};
}
Vous utilisez ensuite comme ceci (avec une importation statique):
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)));
}});
Pour faire ce que vous voulez, vous devez implémenter votre propre matcher. Voilà ce que je piraté en (un peu nulle vérification à l'écart, et bien sûr j'utiliser simplement des interfaces bien connues pour l'échantillon):
@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));
}
}
}
Votre test génère-t-il l'ID? Ou le code sous test le génère-t-il? Les deux attentes se produisent-elles dans le même cas de test? –
le code en cours de test et générer ce oui enfin avec l'exemple ci-dessous, utiliser deux fois, et finnaly comparer l'argument passé, il a travaillé – benzen