Здравствуйте, у меня есть следующий тест, который издевается над Http-вызовами на удаленный сервер, но mocks игнорируются, и реальный сетевой материал запускается Что мне не хватает?Выполнение реального метода вместо mocks
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.
Я не вижу, где вы фактически ** ** использование ваших издевается? – QBrute
Извините, только что обновил вопрос –
Подсказки: A) вы хотите, чтобы мы тратили свое время, чтобы помочь вам, поэтому, пожалуйста, потратьте 1 минуту, чтобы правильно форматировать/отступать весь ваш код. B) err, что именно ваш вопрос. .. извините, но ваши вопросы читаются/выглядят как написанные человеком, задающим самый первый вопрос здесь. – GhostCat