有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

mockito资源内的Java模拟对象

我正在使用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)注释,但这似乎也不起作用

另外,我不想嘲笑刀


共 (0) 个答案