资源内的Java模拟对象

时间:2017-07-06 12:12:26

标签: java mockito powermock dropwizard

我正在使用Dropwizard框架,我正在尝试模拟Web资源中的某些对象(Web资源尝试与外部事物进行通信,我想模拟它)。

目前,我正在我的测试中运行Dropwizard应用程序:

@PowerMockIgnore({"org.apache.http.conn.ssl.*", "javax.net.ssl.*", "javax.crypto.*", "javax.management.*", "javax.net.*"})
@RunWith(PowerMockRunner.class)
@PrepareForTest({ ExternalCommunicator.class})
public class MockTest {
    @ClassRule
    public static final DropwizardAppRule<ApplicationWebserviceConfiguration> RULE = new DropwizardAppRule<>(ApplicationWebserviceApplication.class, ResourceHelpers.resourceFilePath("config.yml"));
    @Before 
    public void doStuff() {
        final ExternalCommunicator externalCommunicator = mock(ExternalCommunicator.class);    
        whenNew(ExternalCommunicator.class).withAnyArguments().thenReturn(externalCommunicator);
}

我的资源看起来像:

@GET
@Path("/{id}")
@Timed
@UnitOfWork
@ApiOperation(value = "Talk to things", notes = "Talk to things.", response = String.class)
public String talk(@ApiParam(value = "id", required = true) @PathParam("id") Long id) {
    ExternalCommunicator communicator = new ExternalCommunicator(id);
    String result = communicator.get();
    someDAO.create(result);
    return result;
}

我正在使用powermock和mockito,但似乎无法正确模拟ExternalCommunicator

我也试过

@Rule
public PowerMockRule rule = new PowerMockRule();

而不是@RunWith(PowerMockRunner.class)注释,但似乎不起作用

另外,我不想嘲笑DAO。

0 个答案:

没有答案