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



你好,我有以下测试模拟对远程服务器的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.
首先,

您需要重构代码,以便它可以注入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();

相关内容

  • 没有找到相关文章

最新更新