无法在 spring 引导项目中使用 SpringJUnit4ClassRunner 运行 powermockrule
Cannot run powermockrule with SpringJUnit4ClassRunner in spring boot project
我有一个 spring 引导项目,需要使用 spring test 运行ner 进行测试(以便我可以获得真实的应用程序上下文)并模拟静态方法。
@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/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
version : null
如何解决这个问题?谢谢!
我从这里找到了修复方法 link
使用 PowerMockRunnerDelegate 而不是 PowerMockRule。
更新后的测试 class 将是:
@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, camel & powermock unittest:
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.........
}
}
在
的情况下建议使用 PowerMockRule
- 使用 Eclemma 和 PowerMockito 推导出代码覆盖率 API
这是一个已知的错误,“PowerMockito 与 Eclmemma 的使用给出了 0% 的覆盖率”,因为字节码操作问题。
因此,
使用
- @RunWith(PowerMockRunner.class) 和
- @PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
当代码覆盖率不是 JUnit 开发期间的主要关注点时。但事实并非如此。
我需要为
定义的测试用例导出代码覆盖率信息
- 静态、私有、最终类型的方法
- Classes with Access Modifier static, private, final
- 在@PrepareForTest
中声明测试Class
因此我不得不使用 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 代理引导”
建议。
我有一个 spring 引导项目,需要使用 spring test 运行ner 进行测试(以便我可以获得真实的应用程序上下文)并模拟静态方法。
@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/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
version : null
如何解决这个问题?谢谢!
我从这里找到了修复方法 link 使用 PowerMockRunnerDelegate 而不是 PowerMockRule。
更新后的测试 class 将是:
@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, camel & powermock unittest:
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.........
}
}
在
的情况下建议使用 PowerMockRule- 使用 Eclemma 和 PowerMockito 推导出代码覆盖率 API
这是一个已知的错误,“PowerMockito 与 Eclmemma 的使用给出了 0% 的覆盖率”,因为字节码操作问题。
因此, 使用
- @RunWith(PowerMockRunner.class) 和
- @PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
当代码覆盖率不是 JUnit 开发期间的主要关注点时。但事实并非如此。
我需要为
定义的测试用例导出代码覆盖率信息- 静态、私有、最终类型的方法
- Classes with Access Modifier static, private, final
- 在@PrepareForTest 中声明测试Class
因此我不得不使用 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 代理引导”
建议。