您好我有以下测试模拟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.
答案 0 :(得分:1)
首先你需要以这样的方式重构代码,它可以注入HttpClient
进行模拟。
protected HttpClient buildHttpClient() {
return HttpClients.custom()./* other config */.build();
}
并更改
HttpClient client = HttpClients.custom().setDefaultHeaders(headers).build();
到
HttpClient client = buildHttpClient();
在你的测试中,
when(underTest.buildHttpClient()).thenReturn(mockClient);
编辑:
MockitoAnnotations.initMocks(UnderTest);
或
@Spy UnderTest underTestClass = new UnderTest();