2017-02-05 12 views
0

こんにちは私は、リモートサーバーへの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

あなたが実際に** **あなたのモックを使うところ、私は表示されないのですか? – QBrute

+0

申し訳ありません、ちょうど質問を更新しました –

+0

ヒント:A)私たちがあなたを助けるために私たちの時間を費やしたいので、あなたのコードを正しくフォーマット/インデントするのに1分を費やしてください。 ..申し訳ありませんが、あなたの質問は、最初の質問をここで聞いている人が書いたようなものです。 – GhostCat

答えて

1

まず、あなたはそれをあざけるためHttpClientを注入することができるようにコードをリファクタリングする必要があります。

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

と変更

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

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

、あなたのテストで

HttpClient client = buildHttpClient();

にEDIT:

MockitoAnnotations.initMocks(UnderTest);

または

@Spy UnderTest underTestClass = new UnderTest(); 
+0

こんにちは、それに変更すると、org.mockito.exceptions.misusing.MissingMethodInvocationExceptionが発生します。 when()には、 '模擬メソッド呼び出し'が必要な引数が必要です。例えば、 (mock.getArticles())when then return(articles); 1. final/private/equals()/ hashCode()メソッドのいずれかをスタブします。 これらのメソッド*はスタブ/検証できません。 2. inside()ではモックでメソッドを呼び出さず、ほかのオブジェクトでメソッドを呼び出します。 3.擬似クラスの親はpublicではありません。 モックエンジンの限界です。 –

+0

しかし、私はそれをテストに変更しました。UnderLyingactor –

+0

チェックを編集、下にあるのは何ですか?私は実際のメソッド呼び出しが起こっていると仮定します。 ??それはまだ正しいですか? –

関連する問題