6

easymock + powermockを使用してテストを作成しています。powermockを使用している理由は、静的メソッドをモックするためです。

私のテストクラスは次のようになります。

import static org.powermock.api.easymock.PowerMock.mockStatic;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(StaticTest.class)
public class DummyTest {
    @Test
    public void testDummy() {
        mockStatic(StaticTest.class);
        Mockito.when(StaticTest.testMethod()).thenReturn("hello world");
        ........
}

これが私のテストの依存関係とバージョンです:

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>1.4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-easymock</artifactId>
        <version>1.4.12</version>
        <scope>test</scope>
    </dependency>

テストを実行すると、次のようにinitializationErrorが発生しました。

java.lang.NoClassDefFoundError: net/sf/cglib/proxy/Enhancer
at org.powermock.api.extension.proxyframework.ProxyFrameworkImpl.isProxy(ProxyFrameworkImpl.java:29)
at org.powermock.reflect.internal.WhiteboxImpl.getUnmockedType(WhiteboxImpl.java:1783)
at org.powermock.reflect.internal.WhiteboxImpl.getType(WhiteboxImpl.java:2237)
at org.powermock.reflect.internal.WhiteboxImpl.setInternalState(WhiteboxImpl.java:327)
at org.powermock.reflect.proxyframework.RegisterProxyFramework.registerProxyFramework(RegisterProxyFramework.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:2014)
at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:885)
at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:859)
at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:466)
at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.registerProxyframework(AbstractTestSuiteChunkerImpl.java:154)
at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.getPowerMockTestListenersLoadedByASpecificClassLoader(AbstractTestSuiteChunkerImpl.java:107)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:144)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:39)
at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createTestDelegators(AbstractTestSuiteChunkerImpl.java:217)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.<init>(JUnit4TestSuiteChunkerImpl.java:59)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.<init>(AbstractCommonPowerMockRunner.java:32)
at org.powermock.modules.junit4.PowerMockRunner.<init>(PowerMockRunner.java:31)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Caused by: java.lang.ClassNotFoundException: net.sf.cglib.proxy.Enhancer
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:143)
at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:67)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 39 more

誰かが以前にこの問題に遭遇したことがありますか?

4

5 に答える 5

3

クラスパスにcglibが必要です。

于 2012-06-27T10:09:11.863 に答える
1

@Premrajで言及されているように、mavenにcglib依存関係を追加すると、powermock1.5.5のinitializationErrorの問題が解決しました。1.4.12 Powermockの場合、おそらくcglibのバージョン2.2.2がそれを行うはずです。

<dependency>
     <groupId>cglib</groupId>
     <artifactId>cglib</artifactId>
     <version>3.1</version>
     <scope>test</scope>
</dependency>
于 2014-08-18T12:06:32.053 に答える
0

私にとって、cglib依存関係の追加は機能しませんでした。私は以下を使用して問題を解決しました:

<!--
    We depend on the EasyMock API but exclude the EasyMock dependency. The reason
    is that PowerMock Whitebox needs a "org.powermock.reflect.spi.ProxyFramework"
    implementation in order to work correctly. This implementation is provided by
    the extension API:s. Since JMock uses CGLib it can reuse the
    "org.powermock.api.extension.proxyframework.ProxyFrameworkImpl" in the
    PowerMock EasyMock API. An alternative to depend on this class would be to simply copy
    the class it into your code (remember that the class must be named
    org.powermock.api.extension.proxyframework.ProxyFrameworkImpl).
-->
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-easymock</artifactId>
    <version>${project.version}</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.easymock</groupId>
            <artifactId>easymock</artifactId>
        </exclusion>
    </exclusions>
</dependency>

ここに示されているように。easymockを使用していないことに注意してください(代わりにmockitoを使用しています)

于 2015-02-26T12:19:54.457 に答える
0

私は複数のオプションを試しましたが、最終的には以下の依存関係が機能しました。スプリングブートアプリケーションで動作するには、PowerMockとMockitoの上位バージョンを使用する必要があります。

以下はMavenの依存関係です。

spring-boot:2.0.1.RELEASE(junitを含む:デフォルトで4.12バージョン)

`1.8 2.0.0-beta.5

</properties>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito2</artifactId>
        <version>${powermock-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-core</artifactId>
        <version>${powermock-version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4-rule</artifactId>
        <version>${powermock-version}</version>
        <scope>test</scope>
    </dependency>'
于 2018-11-21T09:42:38.020 に答える
0

以下の依存関係を追加した後、同じ問題を解決しました。

testCompile('org.powermock:powermock-api-mockito2:2.0.0-beta.5')
testCompile('org.powermock:powermock-module-junit4:2.0.0-beta.5') 
于 2019-04-09T16:13:05.310 に答える