2017-11-10 15 views
1

私はResourceConfig.classにargsのないコンストラクタをモックしようとしています。 それはResourceConfigのは、2つのコンストラクタがあることが起こる:(他のものの中で):複数の一致するコンストラクタPowerMock。 (可変長配列のコンストラクタとno-paramのコンストラクタ)

public ResourceConfig() 
public ResourceConfig(Class... class) 

をPowerMock(1.7.3)右のコンストラクタの取得に失敗しました。 これをバグと見なします。 (?):

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

import static org.powermock.api.mockito.PowerMockito.mock; 
import static org.powermock.api.mockito.PowerMockito.whenNew; 

@RunWith(PowerMockRunner.class) 
@PrepareForTest(ResourceConfig.class) 
public class StackOverflowTest { 


    @Test 
    public void toStackOvflow2() throws Exception { 

    ResourceConfig resConf = mock(ResourceConfig.class); 
    whenNew(ResourceConfig.class).withNoArguments().thenReturn(resConf); 

    //WHATEVER... 
    } 

} 

これが生成します。

org.powermock.reflect.exceptions.TooManyConstructorsFoundException: いくつかのマッチングのコンストラクタは、見つけて下さいおそらくそれにsolutoinが

コードです引数 のパラメータタイプを指定して、PowerMockが のメソッドを参照できるようにします。 .class;
org.glassfish.jersey.server.ResourceConfig()
org.glassfish.jersey.server.ResourceConfig([Ljava.lang.Class:クラス org.glassfish.jersey.server.ResourceConfigあっにおけるマッチングコンストラクタ) org.powermock.reflect.internal.ConstructorFinder.throwExceptionWhenMultipleConstructorMatchesFound(ConstructorFinder.java:89)で

...

任意のアイデア?

答えて

1

あなたは、たとえば、複数のコンストラクタを抑制することができます

@Test 
public void toStackOvflow2() throws Exception { 
    ResourceConfig resConf = mock(ResourceConfig.class); 

    // suppress TooManyConstructorsFoundException 
    MemberModifier.suppress(MemberMatcher.constructorsDeclaredIn(ResourceConfig.class)); 
    whenNew(ResourceConfig.class).withNoArguments().thenReturn(resConf); 

    // verifying that the expected ResourceConfig instance is returned when using the default ctor ... 
    assertSame(resConf, new ResourceConfig()); 
} 

このテストはして渡します。

  • PowerMock 1.7.3
  • ジャージー2.26
関連する問題