java Mockito模拟一个方法调用实际的方法

ddarikpa  于 2023-04-10  发布在  Java
关注(0)|答案(1)|浏览(262)

我一直在尝试模拟一个类的方法。该方法正在从文件中阅读配置。该文件由服务的URI组成。方法正在从文件中读取URI并创建WebTarget对象。我一直在运行testng mockito测试用例,当调用此方法时,我试图从模拟方法返回字符串。问题是每当这个方法被调用时,实际的方法就会被调用,它会试图读取文件。然而,(如果我错了请纠正我)它不应该调用实际的方法并返回模拟的响应。不知何故,它不起作用。下面是测试的代码

@Mock
private ExternalApiConfig externalApiConfig = null;
public CisAccountServiceImpl cisAccoutService;

@BeforeMethod
public void init() {
    MockitoAnnotations.initMocks(this);
    externalApiConfig = Mockito.mock(CisApiConfig.class);
}

private void testUpdateServiceAddressCoordinates(Account account) throws IOException {
    Mockito.when(externalApiConfig.getExternalUrl()).thenReturn("http://localhost:8080/serviceURI/");
    cisAccoutService = new CisAccountServiceImpl();
    assertThat(account, is(notNullValue()));
    List<PremiseExtension> premiseExtensions = account.getPremise().getPremiseExtension();
    assertThat(premiseExtensions.size(), is(30));
    String geoXSettingName = "value2";
    String geoYSettingName = "value3";
    Double lat = 24.0;
    Double lng = -98.0;
    boolean updated = cisAccoutService.updateAccountGeoLocation(accountId,
            lat, lng, geoXSettingName, geoYSettingName);
    assertThat(true, is(updated));
    Account updatedAccount = getAccountById();
    assertThat(Double.parseDouble(updatedAccount.getPremise().getPremiseExtension().get(2).getValue()), is(equals(lat)));
    assertThat(Double.parseDouble(updatedAccount.getPremise().getPremiseExtension().get(3).getValue()), is(equals(lng)));
    //rollback the change
    updatedAccount.getPremise().setPremiseExtension(premiseExtensions);
    updateAccountWithValues(updatedAccount);
}

下面的代码中我想模拟的方法是externalApiConfig.getExternalUrl()

public class CisRestBaseService {

protected WebTarget rootWebTarget;
protected ExternalApiConfig externalApiConfig;

public CisRestBaseService() {
    JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider()
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
            .configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false);

    Client jaxRsClient = ClientBuilder.newClient().register(jacksonJsonProvider);
    this.externalApiConfig = new CisApiConfig();
    this.rootWebTarget = jaxRsClient.target(externalApiConfig.getExternalUrl());
}

}
任何帮助将不胜感激。

h43kikqp

h43kikqp1#

问题是你没有将模拟的externalApiConfig传递给测试的类CisRestBaseService。要解决这个问题,你可以,例如,将它定义为一个构造函数参数(如果需要,你也可以保留当前的构造函数)。

public CisRestBaseService(CisApiConfig config) {
    JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider()
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
            .configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false);

    Client jaxRsClient = ClientBuilder.newClient().register(jacksonJsonProvider);
    this.externalApiConfig = config;
    this.rootWebTarget = jaxRsClient.target(externalApiConfig.getExternalUrl());
}

public CisRestBaseService() {
    this(new CisApiConfig());
}

不,从测试中,你可以像这样示例化服务:

Mockito.when(externalApiConfig.getExternalUrl()).thenReturn("http://localhost:8080/serviceURI/");
cisAccoutService = new CisAccountServiceImpl(externalApiConfig);

因此,您的测试服务将使用mock而不是创建真实的的config对象。这种技术称为“依赖注入”,您可以在这里阅读更多关于它的信息:https://www.digitalocean.com/community/tutorials/java-dependency-injection-design-pattern-example-tutorial

相关问题