4

我有一个 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

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

4

3 回答 3

17

我从此处的链接 中找到了使用 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());
    }
}
于 2015-12-08T18:02:44.260 回答
3

Spring、camel 和 powermock 单元测试:

我对 PowerMockRule 也有同样的问题。我用以下注释替换了它

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)

.

还从 Pom.xml 中删除依赖项 powermock-module-junit4-rule 和 powermock-classloading-xstream 并且它可以工作。

 @RunWith(PowerMockRunner.class)
    @PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = { StaticClassTest.ContextConfig.class })    
    @PrepareForTest({ StaticClass.class })
    @PowerMockIgnore("javax.management.*")
    public class StaticClassTest extends DroolsHelper {

     @Before
    public void setup() {
        PowerMockito.mockStatic(StaticClass.class);

    }

        @Produce(uri = "direct:start")
        private ProducerTemplate template;

        /**
         * 
         * ContextConfig.
         */
        @Configuration
        @Import(AppConfig.class)
        public static class ContextConfig extends SingleRouteCamelConfiguration 
         {

            @Bean
            @Override
            public RouteBuilder route() {
                return new RouteBuilder() {
                    @Override
                    public void configure() {
                        from("direct:start").to("kie:kieSessionType?action=insertBody");
                    }
                };
            }

        }
 @Test
    public void test() {


        PowerMockito.when(StaticClass.anyMethod(Matchers.any(TestClass.class)).thenReturn(false);
        ......assert.........
    }
    }
于 2018-02-09T11:47:40.090 回答
0

建议使用 PowerMockRule 的情况,当

  • 使用 Eclemma 和 PowerMockito API 导出代码覆盖率

由于字节码操作问题, “PowerMockito 和 Eclmemma 的使用提供 0% 的覆盖率”是一个已知错误。

因此,使用

  • @RunWith(PowerMockRunner.class) 和
  • @PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)

当代码覆盖率不是 JUnit 开发期间的主要关注点时。但事实并非如此。

我需要为定义的测试用例导出代码覆盖率信息

  • 静态、私有、最终类型的方法
  • 具有访问修饰符的类 static、private、final
  • 在@PrepareForTest 中声明测试类

因此我不得不使用 PowermockRule。

我不得不删除那些前面提到的注释。

但不知何故,PowerMockRule 的使用对我不起作用。

我添加了下面提到的工件

    <!-- powermock-module-junit4-rule -->
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4-rule</artifactId>
        <version>2.0.2</version>
        <scope>test</scope>
    </dependency>

    <!-- powermock-classloading-xstream -->
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-classloading-xstream</artifactId>
        <version>2.0.2</version>
        <scope>test</scope>
    </dependency>

    <!-- powermock-classloading-base -->
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-classloading-base</artifactId>
        <version>2.0.2</version>
        <scope>test</scope>
    </dependency>

在线建议还包括“使用 Java 代理进行引导”

建议。

于 2021-04-18T11:02:25.947 回答