spring-boot - 无法在 Spring Boot 项目中使用 SpringJUnit4ClassRunner 运行 powermockrule

标签 spring-boot powermock springjunit4classrunner

我有一个 spring boot 项目,需要使用 spring test runner 进行测试(以便我可以获得真实的应用程序上下文)并模拟静态方法。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes= MyApplication.class)
@PrepareForTest(StaticClass.class)
public class StaticClassTest {

    @Rule
    public PowerMockRule rule = new PowerMockRule();

    @Autowired
    HelloCmd hello;

    @Test
    public void testGetOne() {
        mockStatic(StaticClass.class);
        when(StaticClass.getNumber()).thenReturn(2);
        System.out.println(hello.getNumber());
    }
}

运行测试时我收到以下错误消息:

com.thoughtworks.xstream.converters.ConversionException: hello.hystrix.commands.HelloCmd$$EnhancerBySpringCGLIB$$a27be1be : hello.hystrix.commands.HelloCmd$$EnhancerBySpringCGLIB$$a27be1be
---- Debugging information ----
message             : hello.hystrix.commands.HelloCmd$$EnhancerBySpringCGLIB$$a27be1be
cause-exception     : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message       : hello.hystrix.commands.HelloCmd$$EnhancerBySpringCGLIB$$a27be1be
class               : hello.hystrix.commands.StaticClassTest
required-type       : hello.hystrix.commands.StaticClassTest
converter-type      : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path                : /org.powermock.modules.junit4.rule.PowerMockStatement$1/outer-class/fNext/next/next/target/hello
line number         : 15
class[1]            : org.junit.internal.runners.statements.InvokeMethod
class[2]            : org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks
class[3]            : org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks
class[4]            : org.powermock.modules.junit4.rule.PowerMockStatement
class[5]            : org.powermock.modules.junit4.rule.PowerMockStatement$1
version             : null

如何解决这个问题?谢谢!

最佳答案

我从这里找到了修复link 使用 PowerMockRunnerDelegate 而不是 PowerMockRule。

更新后的测试类将是:

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes= MyApplication.class)
@PrepareForTest(StaticClass.class)
public class StaticClassTest {

    @Autowired
    HelloCmd hello;

    @Test
    public void testGetOne() {
        mockStatic(StaticClass.class);
        when(StaticClass.getNumber()).thenReturn(2);
        System.out.println(hello.getNumber());
    }
}

关于spring-boot - 无法在 Spring Boot 项目中使用 SpringJUnit4ClassRunner 运行 powermockrule,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34139478/

相关文章:

java - 如何使用 Powermockito 在公共(public)类中模拟静态方法?

java - 联合测试;是否可以在应用程序上下文启动之前配置 Bean 的默认属性?

java - 为什么在使用 @RunWith(SpringJUnit4ClassRunner.class) 时,JUnit 4.12 会针对 assertEquals 抛出 NoSuchMethodError?

java - 带有 @Transactional 的 EntityManager persist() 不会抛出 DataIntegrityViolationException

java - Spring 测试模拟 findById 不工作

java - 使用返回整数列表的 power mock 测试私有(private)方法

java - 仅使用 @Component 和 @Autowired 使用 SpringJUnit4ClassRunner 进行单元测试

java - Spring Cloud Gateway - 尝试读取 Web 过滤器中的请求正文时请求卡住

java - 如何根据结果内部对象对 mongo 聚合查询进行排序

java - 模拟对象来运行不同的 void 方法