我找不到有关如何使用Android Studio/Gradle设置powermock的任何信息.我尝试过的所有内容都会导致构建异常.
任何人都可以展示出正确的方法吗?
谢谢.
我很难找到设置类的静态字段的方法.它基本上是这样的:
public class Foo{
// ...
private static B b = null;
}
Run Code Online (Sandbox Code Playgroud)
其中B是另一个类.
有什么方法可以在PowerMock中执行此操作而不是使用setInternalStateFromContext()
?使用上下文类方法对于设置一个字段似乎有点过分.
谢谢.
我一直试图弄清楚如何在Junit4中与PowerMock一起运行参数化测试.问题是要使用PowerMock,你需要用你的测试类来装饰
@RunWith(PowerMockRunner.class)
Run Code Online (Sandbox Code Playgroud)
并使用参数化测试你必须装饰
@RunWith(Parameterized.class)
Run Code Online (Sandbox Code Playgroud)
从我所看到的,他们似乎相互排斥!?这是真的?有没有办法解决?我试图在运行PowerMock的类中创建一个参数化类; 这样的事情:
@RunWith(PowerMockRunner.class)
class MyTestClass {
@RunWith(Parameterized.class)
class ParamTestClass {
// Yadayada
}
}
Run Code Online (Sandbox Code Playgroud)
但不幸的是,这并没有太大的好处...... ParamTestClass
仍然没有PowerMock支持(也许不是那么令人惊讶)...而且我有点想法,所以任何帮助都非常感谢!
更新: 对于未来的googlers也会看到:在没有RunWith的情况下使用PowerMock?
我正在设置一个类的静态方法.我必须在@Before
注释的JUnit设置方法中执行此操作.
我的目标是设置类来调用实际方法,除了我明确模拟的那些方法.
基本上:
@Before
public void setupStaticUtil() {
PowerMockito.mockStatic(StaticUtilClass.class);
// mock out certain methods...
when(StaticUtilClass.someStaticMethod(anyString())).thenReturn(5);
// Now have all OTHER methods call the real implementation??? How do I do this?
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,在StaticUtilClass
方法中,public static int someStaticMethod(String s)
不幸的是抛出一个RuntimeException
if null
值.
所以我不能简单地将调用真实方法的明显路线作为默认答案,如下所示:
@Before
public void setupStaticUtil() {
PowerMockito.mockStatic(StaticUtilClass.class, CALLS_REAL_METHODS); // Default to calling real static methods
// The below call to someStaticMethod() will throw a RuntimeException, as the arg is null!
// Even …
Run Code Online (Sandbox Code Playgroud) 鉴于Jacoco在"动态"工具时不能很好地与PowerMockito配合使用,我一直在尝试配置离线工具,希望这能为我提供适用于使用PowerMockito的类的单元测试覆盖率.
我已经将我的pom设置如下,但我的测试课程仍然保持0%的覆盖率.任何帮助都非常感激,因为它让我慢慢疯狂!
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>mandy</groupId>
<artifactId>jacoco-test</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>jacoco-test Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<powermock.version>1.5.4</powermock.version>
<jacoco.version>0.7.1.201405082137</jacoco.version>
</properties>
<dependencies>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<classifier>runtime</classifier>
<version>${jacoco.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>instrument</id>
<phase>process-classes</phase>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>restore-report</id>
<phase>prepare-package</phase>
<goals>
<goal>restore-instrumented-classes</goal>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin> …
Run Code Online (Sandbox Code Playgroud) 我第一次尝试使用Powermock
我使用build.gradle并添加:
dependencies {
...
testCompile 'org.mockito:mockito-all:1.9.5'
testCompile 'org.powermock:powermock-api-mockito:1.5.5'
}
Run Code Online (Sandbox Code Playgroud)
现在我看看我的测试类:
import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.powermock.core.classloader.annotations.PrepareForTest;
@RunWith(PowerMockRunner.class)
@PrepareForTest(GeoUtils.class)
Run Code Online (Sandbox Code Playgroud)
并得到此错误:
@RunWith(PowerMockRunner.class)
^
cannot resolve symbol PowerMockRunner
Run Code Online (Sandbox Code Playgroud)
怎么解决PrepareForTest
而不是PowerMockRunner
?
当尝试使用PowerMockRule技术组合Spring测试运行器和PowerMock运行器时,每当我尝试EntityManager
使用PersistenceContext
JPA中的注释注入时,我都会从Thoughtworks XStream库中获得异常.不使用时,相同的测试工作正常PowerMockRule
.我还在测试开始时忽略了PowerMockLoader中的所有软件包.我尝试了各种值,@PowerMockIgnore
因为这通常解决了我对PowerMock的问题,但是,即使忽略绝对每个包,这个错误仍然会发生.
@PowerMockIgnore("*")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/test-configuration.xml")
public class SpringAndPowerMockTest {
@Rule
public PowerMockRule rule = new PowerMockRule();
@PersistenceContext
private EntityManager manager;
@Test
public void test() {
}
}
Run Code Online (Sandbox Code Playgroud)
例外情况如下 - 链接到pastebin上的完全回溯,回溯超出了问题长度的限制):
相关持久单元:
<persistence-unit name="inMemory">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="org.h2.Driver" />
<property name="hibernate.connection.url" value="jdbc:h2:mem:InMemoryUnitTests;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE" />
<property name="hibernate.connection.username" value="sa" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.hbm2ddl.auto" …
Run Code Online (Sandbox Code Playgroud) 我正在junit
使用PowerMock
测试运行器运行测试用例.我正在使用以下命令行来执行它:
java -cp .:junit-4.9b2.jar:easymock-3.0.jar:powermock-easymock-1.4.8-full.jar org.junit.runner.JUnitCore SampleTest
Run Code Online (Sandbox Code Playgroud)
这样做时,我收到此错误:
initializationError(SampleTest)
java.lang.NoClassDefFoundError: org/junit/internal/runners/TestClassRunner
...
Run Code Online (Sandbox Code Playgroud)
我该如何解决?
我尝试将junit,mokito和powermock设置在一起,但是当我运行测试时,我得到了ClassNotFoundException :(
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:2.7.22'
androidTestCompile 'org.mockito:mockito-core:2.7.22'
androidTestCompile "org.mockito:mockito-android:2.7.22"
testCompile 'org.robolectric:robolectric:3.3.2'
testCompile 'org.hamcrest:hamcrest-core:1.3'
testCompile 'org.powermock:powermock-core:1.6.6'
testCompile 'org.powermock:powermock-module-junit4:1.6.6'
testCompile 'org.powermock:powermock-module-junit4-rule:1.6.6'
testCompile 'org.powermock:powermock-api-mockito:1.6.6'
testCompile 'org.powermock:powermock-classloading-xstream:1.6.6'`
Run Code Online (Sandbox Code Playgroud)
我还尝试添加cglib:
testCompile 'cglib:cglib:3.1'
testCompile 'cglib:cglib-nodep:3.1'
Run Code Online (Sandbox Code Playgroud)
但没有缺乏.
任何人都可以分享工作配置或指出我的错误.
我运行测试时的堆栈跟踪:
Exception in thread "main" java.lang.NoClassDefFoundError: org/mockito/exceptions/Reporter
at sun.reflect.GeneratedSerializationConstructorAccessor5.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:48)
at org.powermock.reflect.internal.WhiteboxImpl.newInstance(WhiteboxImpl.java:251)
at org.powermock.reflect.Whitebox.newInstance(Whitebox.java:139)
at org.powermock.api.extension.reporter.AbstractMockingFrameworkReporterFactory.getInstanceForClassLoader(AbstractMockingFrameworkReporterFactory.java:41)
at org.powermock.api.extension.reporter.AbstractMockingFrameworkReporterFactory.create(AbstractMockingFrameworkReporterFactory.java:35)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.getMockingFrameworkReporter(JUnit4TestSuiteChunkerImpl.java:140)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:119)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498) …
Run Code Online (Sandbox Code Playgroud) 我正在尝试为 Junit 5 测试用例模拟静态类(org.apache.commons.beanutils.Beanutils)。我发现mockito-inline
依赖有助于模拟静态类。我尝试在项目中使用 ,mockito-inline
由于一些奇怪的原因,它在没有库的情况下给我编译错误mockito-core
。
我mockito-core
正在下面:
org.mockito.exceptions.base.MockitoException:
The used MockMaker PowerMockMaker does not support the creation of static mocks
Mockito's inline mock maker supports static mocks based on the Instrumentation API.
You can simply enable this mock mode, by placing the 'mockito-inline' artifact where you are currently using 'mockito-core'.
Note that Mockito's inline mock maker is not supported on Android.
at com.xx.xx.xx.AvroCopyPropertiesInvocationTargetExceptionScenario(CreditOfferServiceTest.java:1197)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:675)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:125)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:132)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:124) …
Run Code Online (Sandbox Code Playgroud)