2017-09-26 14 views
0

IはPowerMockitoとモックオブジェクトへの入力に渡されたパラメータを捕捉しようとすると、これはコードされている:PowerMockitoモックオブジェクトに渡されたパラメータを取得する方法は?

//I create a mock object 
ClassMocked mock = PowerMockito.mock(ClassMocked.class); 

//Create the captor that will capture the String passed in input to the mock object 
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class); 

//When the method put is executed on my mock I want the second parameter (that is a String) to be captured 
Mockito.verify(mock).put(anyString(), inputDataMapCaptor.capture()); 

//Creates the instance of the class that I want to test passing the mock as parameter 
ClassToTest instance = new ClassToTest(mock); 

//Executes the method that I want to test 
instance.methodToTest(); 

/* I know that during the execution of "methodToTest()" mock.put(String,String) will be executed and I want to get the second string parameter. */ 

Iテストを実行すると、私はラインMockito.verify(モック)を実行例外を有します.put(...);

"募集していますが呼び出されていないmock.put(any、キャプチャ引数);"

どうしたのですか?

答えて

2

あなたはinstance.methodToTest();

Mockito.verify(mock).put(anyString(), inputDataMapCaptor.capture());
+0

はい、あなたは正しいです、私は後に検証を入れて、それは動作しますが、私はどのように動作するのか迷っています。 メソッドを検証して実行をテストしたら、模擬的にパラメータを取得する必要があることがわかりますか? – user2572526

+0

PowerMockitoの内部についてはわかりませんが、モックのメソッドへのすべての呼び出しをパラメータで記録する必要があります – chomnoue

1

verify()を呼び出す必要が検証し、指定されたメソッド呼び出しが行わなかったこと。

"upfront"メソッド呼び出しが行われるはずがないことを確認することはできません。

したがって:verify()は、の後、の後に発生する必要があります。

+0

あなたの答えは正しいです。彼が最初に答えたので、ユーザーchomnoueの1人を受け入れました。 – user2572526

関連する問題