gpt4 book ai didi

java - 真正的方法执行而不是模拟

转载 作者:行者123 更新时间:2023-12-01 08:55:41 25 4
gpt4 key购买 nike

您好,我有以下测试,模拟对远程服务器的 Http 调用,但模拟被忽略,真正的网络内容被执行我错过了什么?

public class MyTest {

static ActorSystem system;
JavaTestKit senderProbe;
JavaTestKit jobRunnerProbe;
TestActorRef<RoutesManager> underTest;
static Service webapp3;
static JSONObject job;
static JSONArray portsArray;
static JSONArray routesArray;
static JSONObject routeObject;
private static final RoutesManager routeManager = mock(RoutesManager.class);
private static final HttpClient client = mock(DefaultHttpClient.class);
private static final HttpGet get = mock(HttpGet.class);
private static final HttpResponse response = mock(CloseableHttpResponse.class);
private static final HttpEntity entity = mock(HttpEntity.class);
private static final InputStream inputStream = mock(InputStream.class);


@BeforeClass
public static void setup() throws ClientProtocolException, IOException, JSONException {

system = ActorSystem.create();
}


@AfterClass
public static void teardown() {
JavaTestKit.shutdownActorSystem(system);
system = null;
}



@Before
public void makeActorUnderTest() throws ClientProtocolException, ParseException, IOException, JSONException {
senderProbe = new JavaTestKit(system);
jobRunnerProbe = new JavaTestKit(system);
String token = JobRunner.getAuthToken(TestResources.AUTH_ENDPOINT, TestResources.APCERA_USER, TestResources.APCERA_PASSWORD);
underTest = new TestActorRef<RoutesManager>(system,
Props.create(RoutesManager.class,
token, webapp3, apcSession),
senderProbe.getRef(), UUID.randomUUID().toString());

HttpGet getRoute = new HttpGet("actual Api");
Header header = // set header
JSONObject routesJson = new JSONObject();
List<String> jobids = new ArrayList<String>();
jobids.add("jobuuid");
routesJson.put(webapp3.getRoute(), jobids);
Mockito.when(routeManager.buildHttpClient(token)).thenReturn(client);
Mockito.when(routeManager.buildHttpGet(webapp3)).thenReturn(getRoute);

Mockito.when(client.execute(getRoute)).thenReturn(response);
Mockito.when(response.getEntity()).thenReturn(entity);
Mockito.when(entity.getContent()).thenReturn(inputStream);
Mockito.when(entity.getContent().toString()).thenReturn(routesJson.toString());
MockitoAnnotations.initMocks(this);


}

@Test

public void myTest() throws ClientProtocolException, ParseException, IOException, JSONException {
underTest.tell(new \triggeringMsg,underTest.underlyingActor().token), senderProbe.getRef());
senderProbe.watch(underTest);
senderProbe.expectTerminated(Duration.create(100, TimeUnit.SECONDS),
underTest);

}

}

以及源代码

    //Find all jobs that share a common route between them
if (msg instanceof triggering message) {
HttpClient client = buildHttpClient(message.token);
HttpGet get = buildHttpGet(message.service);

HttpResponse response = client.execute(get);
String json_string_response = EntityUtils.toString(response.getEntity());
if (!json_string_response.isEmpty()) {
delegate(new JSONObject(json_string_response), message.service, message.token);
getSelf().tell(PoisonPill.getInstance(), getSelf());

}

}

else {
//unhandled
}

以及堆栈跟踪

JSONObject["value set in the set up of test"] not found.
org.json.JSONException: JSONObject["] not found.

最佳答案

首先,您需要重构代码,使其可以注入(inject) HttpClient 进行模拟。

protected HttpClient buildHttpClient() {
return HttpClients.custom()./* other config */.build();
}

并改变

HttpClient client = HttpClients.custom().setDefaultHeaders(headers).build();

HttpClient 客户端 = buildHttpClient();

在您的测试中,

when(underTest.buildHttpClient()).thenReturn(mockClient);

编辑:

MockitoAnnotations.initMocks(UnderTest);

@Spy  UnderTest underTestClass = new UnderTest();

关于java - 真正的方法执行而不是模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42052803/

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