简体   繁体   English

如何模拟接受类类型的通用方法?

[英]How do I mock a generic method that accepts a class type?

I'm trying to write a unit test for a REST API client. 我正在尝试为REST API客户端编写单元测试。 I am following a pattern that has worked well for me in a variety of other unit tests. 我遵循的模式在各种其他单元测试中对我来说都很有效。 In particular, I have successfully mocked dependencies that have been injected into a repository under test. 特别是,我已经成功地模拟了已注入到受测存储库中的依赖项。 However, when I come to mock a Spring RestTemplate , I cannot find a way to get its getForObject() method to return anything other than null . 但是,当我模拟Spring RestTemplate ,我无法找到一种方法来获取其getForObject()方法以返回null以外的任何null Does anyone know how to do this? 有谁知道如何做到这一点? I suspect the issue might be that the signature for RestTemplate.getForObject() contains generics: 我怀疑问题可能在于RestTemplate.getForObject()的签名包含泛型:

public <T> T getForObject(URI url, Class<T> responseType) throws RestClientException

Here is my REST client class that I am trying to test: 这是我要测试的REST客户端类:

@Repository
public class WebApplicationClient {
    private final RestTemplate template;
    public WebApplicationClient(RestTemplate template) {
        this.template = template;
    }
    public <T> T getData(String baseUrl, Class<T> clazz) {
        String endpoint = process(baseUrl);
        try {
            return template.getForObject(endpoint, clazz);  // Mock this call during testing
        } catch (HttpClientErrorException | HttpServerErrorException e) {
            String msg = "API call failed: " + endpoint;
            LOG.warn(msg, e);
            throw new WebApplicationException(e.getStatusCode(), msg, e);
        }
    }
}

Here is my unit test so far. 到目前为止,这是我的单元测试。 What ever I try for when(template.getForObject(...)) always returns null . 我尝试的when(template.getForObject(...))总是返回null Hence result is always null and my assertion fails. 因此result始终为null并且我的断言失败。

public class WebApplicationClientUnitTests {
    @Mock private RestTemplate template;
    private WebApplicationClient client;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        client = new WebApplicationClient(template);
    }

    @Test
    public void getData_Test1() {
        // when(template.getForObject(any(), eq(String.class))).thenReturn("sample"); // Returns null
        when(template.getForObject(any(), any())).thenReturn("sample"); // Returns null

        String result = client.getData(TEST_URL, "db", expectedState, String.class);
        Assert.assertEquals("sample", result);
    }
}

How do I get getForObject() to return an actual value? 如何获取getForObject()以返回实际值?

@Test
public void getData_Test1() {

    when(template.getForObject((String) any(),eq(String.class))).thenReturn("sample");
    //OR
    //when(template.getForObject((String) any(),(Class)any())).thenReturn("sample");
    //OR
    //when(template.getForObject(any(String.class), any(Class.class))).thenReturn("sample");

    String result = client.getData("TEST_URL", String.class);
    Assert.assertEquals("sample", result);
}

The Above code works fine for me. 上面的代码对我来说很好用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在调用接受Class <T>并返回T的泛型方法时,如何返回某个类型的列表? - How can I return a list of a certain type when calling a generic method that accepts a Class<T> and returns a T? Mockito 模拟方法调用,接受通用 class 作为参数 - Mockito mock method call that accepts generic class as argument 如何使用接受任何类型的 ArrayList 的泛型方法并在指定类型的类中使用该方法? - How to use generic method that accepts an ArrayList of any type and use the method in the specified type class? 如何在JMockit中使用通用对象模拟方法? - How do I mock a method with generic objects in JMockit? 如何在JMockit中模拟具有void返回类型的方法? - How do I mock a method with void return type in JMockit? 如何从可测试类中模拟调用方法? - How do I mock call method from testable class? 如何模拟类成员字段的方法? - How do I mock a method of a class' member field? 如何使用EasyMock模拟从抽象类继承的方法? - How do I mock a method inherited from an abstract class with EasyMock? 如何在类中使用扩展的泛型类型的类型? - How do I use an extended generic type's type in a class? Java:如何强制 class 类型参数与泛型方法中指定的泛型类型相同? - Java: How do you enforce a class type parameter to be the same as generic type specified in a generic method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM