gpt4 book ai didi

java - 然而,当其他实例也使用时,JUnit/Mockito 验证 `any(HttpPut.class)` 会通过

转载 作者:行者123 更新时间:2023-11-30 01:48:12 26 4
gpt4 key购买 nike

我有一个服务类,它调用 REST API 来获取、创建、更新和删除订阅者。 Uri 保持不变,但 HTTP 方法如您所料发生变化。我想测试给出的方法是否正确。下面是 updateSubscriber 及其测试的示例。

public class MyService {

HttpClient httpClient;

public MyService(HttpClient httpClient) {
this.httpClient = httpClient;
}

//...

public int updateSubscriber(Subscriber subscriber) throws ... {

// PUT is the correct method for this request
HttpResponse response = httpClient.execute( new HttpPut( "https://example.org/api/subscribers" ) );

//...
}

//...

这是我使用 JUnit 和 Mockito 进行的测试:

@RunWith(MockitoJUnitRunner.class)
public class MyServiceTest
{

@Mock
private HttpClient mockHttpClient;

@Mock
private HttpResponse mockResponse;

@Mock
private StatusLine mockStatusline;

@Mock
private HttpEntity mockEntity;

// test subject
private MyService myService;

@Before
public void setup() {

// // this will just ensure http* objects are returning our mocked instances so we can manipulate them..
// when(mockHttpClient.execute(any(HttpGet.class))).thenReturn(mockResponse);
// when(mockHttpClient.execute(any(HttpPost.class))).thenReturn(mockResponse);
// when(mockHttpClient.execute(any(HttpPut.class))).thenReturn(mockResponse);
// when(mockHttpClient.execute(any(HttpDelete.class))).thenReturn(mockResponse);

// when(mockResponse.getStatusLine()).thenReturn(mockStatusline);
// when(mockStatusline.getStatusCode()).thenReturn(HttpStatus.SC_OK);

myService = new MyService(mockHttpClient);
}

@Test
public void testUpdateSubscriber() throws ...
{

when(mockHttpClient.execute(any(HttpPut.class))).thenReturn(mockResponse);

when(mockResponse.getStatusLine()).thenReturn(mockStatusline);
when(mockStatusline.getStatusCode()).thenReturn(HttpStatus.SC_OK);

String responseString = "...";

// this is consumed by a static method which we cannot mock, so we must deal with an actual entity instance
BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(new ByteArrayInputStream(responseString.getBytes()));
when(mockResponse.getEntity()).thenReturn(entity);

// create a test case Subscriber instance
Subscriber subscriber = new Subscriber();

int statusCode = myService.updateSubscriber(subscriber);

assertEquals(HttpStatus.SC_OK, statusCode);

// just confirm that an HTTP request was made
// TODO this isn't working, still passes when wrong Http* method used
verify(mockHttpClient, times(1)).execute(any(HttpPut.class));
}

//...

但是,当我(错误地)拥有另一个 Http* 方法实例时,它仍然会通过:

// this is wrong, and should fail, but passed :(
HttpResponse response = httpClient.execute( new HttpGet( "https://example.org/api/subscribers" ) );

我真的很希望能够对此进行测试,因为如果方法错误,执行的操作可能是错误的。此测试是为了确保 PUT 方法正确地与 updateSubscriber 的 HTTP 请求一起使用。有什么想法吗?

最佳答案

测试通过,因为HtppPutHttpGet都是HttpRequestBase的实现类,从HttpRequestBase类更改mocking到 HttpPut

 when(mockHttpClient.execute(any(HttpPut.class))).thenReturn(mockResponse);

所以现在,如果您尝试使用 GET 调用,测试将因 NullPointerException 失败,因为 GET 调用没有 stub

关于java - 然而,当其他实例也使用时,JUnit/Mockito 验证 `any(HttpPut.class)` 会通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57115787/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com