5

特定のコントローラーをテストするクラスがあり、正常に動作します

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:config/test-applicationContext-config.xml")
@TestExecutionListeners({ 
    WebContextTestExecutionListener.class, 
    DependencyInjectionTestExecutionListener.class,
    DirtiesContextTestExecutionListener.class })
public class TestAdminController {  
     //.....
}

私はそこでmockito.orgのGoogleのMockitoライブラリを使用て、基になるBeanをモックしました。

今私の質問は次のとおりです。モックする必要のあるクラスがいくつかありますが、それらにはfinalメソッドがあり、Googleのモックはこの問題を解決していないようです。私の同僚の1人が、powermock.orgのPowermockを使用することを提案しました。ただし、テスタークラスにアノテーションを付ける必要があり@RunWith(PowerMockRunner.class)ます。これを使用する場合、アノテーションを削除する必要があります@RunWith(SpringJUnit4ClassRunner.class)。これにより、Springテストコンテキストが作成されないという問題が発生します。この状況を回避するにはどうすればよいですか?

提案後、注釈PowerMockRuleの代わりに使用するように構成しました@RunWith

私のプロジェクトMavenの依存関係は次のとおりです

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-mockito-release-full</artifactId>
    <version>1.5</version>
    <type>pom</type>
</dependency> 
<dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4-rule</artifactId>
        <version>1.5</version>
        <scope>test</scope>
</dependency>
<dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-classloading-xstream</artifactId>
        <version>1.5</version>
        <scope>test</scope>
</dependency>

今、私のクラスは次のようになっています(別のコントローラーテスト)

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:config/test-applicationContext-config.xml")
@TestExecutionListeners({ 
    WebContextTestExecutionListener.class, 
    DependencyInjectionTestExecutionListener.class,
    DirtiesContextTestExecutionListener.class })
@PrepareForTest(ADSynchronizationImpl.class)
public class ThirdPartyLoginControllerTest {
    @Rule
    public PowerMockRule rule = new PowerMockRule();

    @Autowired
    private ThirdPartyLoginController thirtPartyLoginController;

    @Autowired
    private MockFactoryForControllers mockFactory;

    @Test
    public void testLoginActiveDirectory(){

    }
}

しかし、テストを実行した後、次の例外トレースが表示されます

com.thoughtworks.xstream.converters.ConversionException:javassist.NotFoundException:com.somecompany.analytics.manager.db.api.MlDao $$ EnhancerByMockitoWithCGLIB $$ b86d7415:javassist.NotFoundException:com.somecompany.analytics.manager.db.api。 MlDao $$ EnhancerByMockitoWithCGLIB $$ b86d7415 ----デバッグ情報----メッセージ:javassist.NotFoundException:com.somecompany.analytics.manager.db.api.MlDao $$ EnhancerByMockitoWithCGLIB $$ b86d7415原因-例外:java.lang。 RuntimeExceptionの原因メッセージ:javassist.NotFoundException:com.somecompany.analytics.manager.db.api.MlDao $$ EnhancerByMockitoWithCGLIB $$ b86d7415クラス:com.somecompany.ui.controllers.ThirdPartyLoginController必須タイプ:com.somecompany.ui.controllers .ThirdPartyLoginControllerコンバータータイプ:com.thoughtworks。xstream.converters.reflection.ReflectionConverterパス:/org.powermock.modules.junit4.rule.PowerMockStatement $ 1 / external-class / fNext / fTarget / thirtPartyLoginController / mlDao行番号:14クラス1 :com.somecompany.ui.controllers.ThirdPartyLoginControllerTestクラス2 :org.junit.internal.runners.statements.InvokeMethod class [3]
:org.powermock.modules.junit4.rule.PowerMockStatement class [4]
:org.powermock。 modules.junit4.rule.PowerMockStatement$1バージョン
:null ------------------------------- at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java: 79)com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)at com.thoughtworks.xstream.converters.reflection .AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)at com.thoughtworks.xstream.converters.Reflect.AbstractRefl :234)com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)atcom。thinkworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshall AbstractReflectionConverter.java:355)at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter。 .thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)atcom.thoughtworks.xstream.core。TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal 306)com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)at com.thoughtworks.xstream.core .AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)atcom.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter。unmarshallField(AbstractReflectionConverter.java:355)at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(Abs com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother (TreeUnmarshaller.java:66)at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)atcom.thoughtworks。 xstream.core。AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1052)at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1036)at com.thoughtworks.xstream .XStream.fromXML(XStream.java:912)at com.thoughtworks.xstream.XStream.fromXML(XStream.java:903)at org.powermock.classloading.DeepCloner.clone(DeepCloner.java:54)atorg.powermock。 classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:89)at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49) org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)でorg。springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)atorg.springframework.test。 context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)at org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:238)at org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:63)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)at org.junit.runners.ParentRunner.access $ 000(ParentRunner.java: 53)org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229)atorg.springframework.test.context.junit4.statements。RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)at org.eclipse.jdt .internal.junit.runner.TestExecution.run(TestExecution.java:38)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)at org.eclipse.jdt.internal.junit .runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)原因:java.lang.RuntimeException:javassist.NotFoundException:com.somecompany.idm.analytics.manager.db。 api.MlDao $$ EnhancerByMockitoWithCGLIB $$ b86d7415 at org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java:199)at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:149)atorg。 powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:67)at java.lang.ClassLoader.loadClass(ClassLoader.java:356)at com.thoughtworks.xstream.core.util.ClassLoaderReference.loadClass(ClassLoaderReference.java: 31)com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:49)のcom.thoughtworksで。xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)at com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:55)at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java: 30)com.thoughtworks.xstream.mapper.PackageAliasingMapper.realClass(PackageAliasingMapper.java:88)at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)at com.thoughtworks.xstream.mapper.ClassAliasingMapper .realClass(ClassAliasingMapper.java:79)at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)atcom。 thinkworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)(com.thoughtworks.xstream.mapper.MapperWrapper)。realClass(MapperWrapper.java:30)at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)at com.thoughtworks .xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:74)at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java :30)com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)atcom.thoughtworks.xstream.mapper。 MapperWrapper.realClass(MapperWrapper.java:30)at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)atcom.thoughtworks.xstream。mapper.MapperWrapper.realClass(MapperWrapper.java:30)at com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:45)at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.determineType(AbstractReflectionConverter.java: 458)com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:294)at com.thought .core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)... 57 more原因:javassist.NotFoundException:com.somecompany.analytics.manager.db.api.MlDao $$ EnhancerByMockitoWithCGLIB $$ b86d7415 at javassist.ClassPool.get (ClassPool.java:450)org.powermock.coreで。classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java:192)...87詳細

4

3 に答える 3

6

PowerMockとの使用方法を説明しているこの記事SpringJUnit4ClassRunnerをご覧ください。基本的には、特別なPowerMockRuleルールを使用する必要があります。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:config/test-applicationContext-config.xml")
@TestExecutionListeners({ 
    WebContextTestExecutionListener.class, 
    DependencyInjectionTestExecutionListener.class,
    DirtiesContextTestExecutionListener.class })
public class SpringExampleTest {

    @Rule
    public PowerMockRule rule = new PowerMockRule();

}

編集:他の人があなたと同じ問題を抱えていた@PowerMockRuleようです。代わりにPowerMockAgentを使用すると、ケースを解決できます。SOに関するこの回答も参照してください。

于 2013-03-05T13:18:01.093 に答える
6

テストクラスに注釈を付けることもできます

@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)

こちらの例をご覧ください。

于 2016-08-23T09:47:52.397 に答える
3

Powermockの代わりにJMockitを使用できます。Powermockが実行できるすべてのことを実行でき、@ RunWith句を必要としないため、Springテストランナーに干渉しません。PowerMockRuleが解決したよりも多くの問題を引き起こしたことがわかりました...

于 2013-09-25T13:03:34.543 に答える