Java 使用Powermock测试Spring控制器
我有一个测试特定控制器的类,它运行良好Java 使用Powermock测试Spring控制器,java,spring-mvc,mockito,powermock,spring-test-mvc,Java,Spring Mvc,Mockito,Powermock,Spring Test Mvc,我有一个测试特定控制器的类,它运行良好 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:config/test-applicationContext-config.xml") @TestExecutionListeners({ WebContextTestExecutionListener.class, DependencyInjectionTestExe
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:config/test-applicationContext-config.xml")
@TestExecutionListeners({
WebContextTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class })
public class TestAdminController {
//.....
}
我从年开始使用Google的Mockito库来模拟我的底层bean
现在我的问题是:我有一些类也需要模拟,但它们有final
方法,谷歌的mockito似乎无法解决这个问题。我的一位同事建议从中使用Powermock。但是它需要用@RunWith(PowerMockRunner.class)
注释来注释tester类。如果我使用它,我必须删除注释@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>
但是在运行测试之后,我得到了以下异常跟踪
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.powermock语句$1/outer class/fNext/fTarget/thirtPartyLoginController/mlDao
行号:14班:
com.somecompany.ui.controllers.ThirdPartyLoginControllerTest类
:org.junit.internal.runners.statements.InvokeMethod类[3]:org.powermock.modules.junit4.rule.powermock语句类[4]
:org.powermock.modules.junit4.rule.powermock语句$1版本
:null -------------------------------位于com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79) 在 com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65) 在 com.thoughtworks.xstream.core.TreeUnmarshaller.convertOther(TreeUnmarshaller.java:66) 在 com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355) 在 com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306) 在 com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234) 在 com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) 在 com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65) 在 com.thoughtworks.xstream.core.TreeUnmarshaller.convertOther(TreeUnmarshaller.java:66) 在 com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355) 在 com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306) 在 com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234) 在 com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) 在 com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65) 在 com.thoughtworks.xstream.core.TreeUnmarshaller.convertOther(TreeUnmarshaller.java:66) 在 com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355) 在 com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306) 在 com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234) 在 com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) 在 com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65) 在 com.thoughtworks.xstream.core.TreeUnmarshaller.convertOther(TreeUnmarshaller.java:66) 在 com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355) 在 com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306) 在 com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234) 在 com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) 在 com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65) 在 com.thoughtworks.xstream.core.TreeUnmarshaller.convertOther(TreeUnmarshaller.java:66) 在 com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50) 在 com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134) 在 com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32) 位于com.thoughtworks.xstream.xstream.unmarshal(xstream.java:1052) com.thoughtworks.xstream.xstream.unmarshal(
@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(){
}
}
@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();
}
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)