java在Dropwizard的不同状态下使用不同的模拟
我正在尝试使用运行Dropwizard的应用程序设置pact提供程序,但在为不同的状态使用不同的模拟时遇到问题。在Dropwizard中是否有类似于Spring的@MockBean的方法,或者其他方法来实现这一点
这是我的提供者类:
@RunWith(PactRunner.class)
@Provider("my-proxy")
@PactFolder("src/test/java/pact/pacts")
public class UserContractTest {
private static final String CONFIG_PATH = ResourceHelpers.resourceFilePath("config.yml");
// I want to do the commented out code in Dropwizard with Dropwizard equivalents
// @MockBean
// private MyClass myMockedClass;
@ClassRule
public static final DropwizardAppRule<MyAppConfiguration> RULE =
new DropwizardAppRule<>(MyAppMock.class, CONFIG_PATH);
@TestTarget
public final Target target = new HttpTarget(RULE.getLocalPort());
@State("default")
public void toDefaultState(Map<String, String> params) {
if(params.get("someState").equals("stateOne")) {
// when(myMockedClass.someFunc()).thenReturn("someAnswerForStateOne");
} else {
// when(myMockedClass.someFunc()).thenReturn("someAnswerForAllOtherStates");
}
}
}
# 1 楼答案
最后,我在请求参数上使用了argThat,以便能够在同一个方法上使用两个不同的方法模拟