2011-02-10 68 views
11

これはテストです:クラスを取るコンストラクタでオブジェクトをモックする方法は?

import static junit.framework.Assert.assertTrue; 
import static org.powermock.api.mockito.PowerMockito.mock; 
import static org.powermock.api.mockito.PowerMockito.whenNew; 

import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.powermock.core.classloader.annotations.PrepareForTest; 
import org.powermock.modules.junit4.PowerMockRunner; 

@RunWith(PowerMockRunner.class) 
@PrepareForTest({ClassUnderTesting.class}) 
public class ClassUnderTestingTest { 

    @Test 
    public void shouldInitializeMocks() throws Exception { 
     CollaboratorToBeMocked mockedCollaborator = mock(CollaboratorToBeMocked.class); 

      suppress(constructor(CollaboratorToBeMocked.class, InjectedIntoCollaborator.class)); 

     whenNew(CollaboratorToBeMocked.class) 
      .withArguments(InjectedAsTypeIntoCollaborator.class) 
      .thenReturn(mockedCollaborator); 

     new ClassUnderTesting().methodUnderTesting(); 

     assertTrue(true); 
    } 
} 

これらはクラスである:

public class ClassUnderTesting { 

    public void methodUnderTesting() { 
     new CollaboratorToBeMocked(InjectedAsTypeIntoCollaborator.class); 
    } 

} 

public class CollaboratorToBeMocked { 

    public CollaboratorToBeMocked(Class<InjectedAsTypeIntoCollaborator> clazz) { 
    } 

    public CollaboratorToBeMocked(InjectedIntoCollaborator someCollaborator) { 
    } 

    public CollaboratorToBeMocked() { 
    } 

} 

public class InjectedAsTypeIntoCollaborator { 

} 

public class InjectedIntoCollaborator { 

} 

これはエラーです:どのように私はPowerMockを行うことができます。

org.powermock.reflect.exceptions.TooManyConstructorsFoundException: Several matching constructors found, please specify the argument parameter types so that PowerMock can determine which method you're refering to. 
Matching constructors in class CollaboratorToBeMocked were: 
CollaboratorToBeMocked(InjectedIntoCollaborator.class) 
CollaboratorToBeMocked(java.lang.Class.class) 

ここ質問が来ます探しているコンストラクタを見つけますか?

問題のある行は、suppressです。それがエラーの原因です。

+1

CollaboratorToBeMocked(java.lang.Class.class)コンストラクターを削除するとどうなりますか?それはその後動作しますか? – Davidann

+0

他のコンストラクタを削除すると、InjectedIntoCollaboratorでコンストラクタを削除すると、それは動作します。 – Belun

答えて

2

質問を書くまで、私はPowerMockを知りませんでしたが、いくつかの読書をして、彼らのドキュメントでこれを見つけました。

スーパークラスは、いくつかの コンストラクタを持っている場合は、それが唯一の特定の 1を抑制するために PowerMockを伝えることが可能です:それはあなたを助けている場合それでも私は本当にわかりません。 ClassWithSeveralConstructorsというクラスがあり、 のコンストラクタが1つの文字列 と、もう1つのコンストラクタが のintを引数にとり、 のみがStringコンストラクタを抑制したいとします。 suppress(constructor(ClassWithSeveralConstructors.class, String.class)); メソッドを使用してこれを行うことができます。 http://code.google.com/p/powermock/wiki/SuppressUnwantedBehavior

で発見

それはあなたが望んだことではないですか?

EDIT:今、あなたはすでに抑制を試みています。しかし、あなたは抑圧のコールを持っていると確信していますか? constructor()の最初の引数は、コンストラクタを抑制するクラスではないでしょうか?

+0

あなたは正しいです。私の憂鬱は混乱です。それにもかかわらず、それでも私は 'suppress(コンストラクタ(CollaboratorToBeMocked.class、InjectedIntoCollaborator.class)); ' – Belun

+0

umを使用しました。テストクラスを修正したら、テストは緑色になりました。だから、問題は、今、憂鬱です。バグかもしれません... – Belun

+0

完全な例があり、それでも期待される結果が見えない場合は、間違いなくバグレポートをプロジェクトに提出する必要があります。 –

17

ご質問にはおそらく遅すぎます。私は今日それに会い、次のURLで解決策を見つけました。基本的に、あなたはあなたの引数型を指定する必要があります。

whenNew(MimeMessage.class).**withParameterTypes(MyParameterType.class)**.withArguments(isA(MyParameter.class)).thenReturn(mimeMessageMock); 

http://groups.google.com/group/powermock/msg/347f6ef1fb34d946?pli=1

それはあなたを助けることができると思います。 :)

+1

この回答は私のために働いた。私はジェネリックTを持つResponseEntityクラスの状況がありました。 公共ResponseEntity(MultiValueMap <文字列、文字列>ヘッダ、はhttpStatusからstatusCode) 公共ResponseEntity(T本体、はhttpStatusからstatusCode) 次の構文を使用する場合: PowerMockito.whenNew(ResponseEntity.class).withArguments(ヘッダ、からstatusCodeを)。 thenReturn(responseEntity); これは動作しませんが、 PowerMockito.whenNew(ResponseEntity.class).withParameterTypes(MultiValueMap.class、HttpStatus.class).withArguments(headers、statusCode).thenReturn(responseEntity); 作品 – gaoagong

関連する問題