Test mechanism error and initialization error

1,865 views
Skip to first unread message

Mikael

unread,
Mar 24, 2011, 5:51:18 AM3/24/11
to PowerMock
Hello
I'm a beginner with PowerMock and Mockito and get two strange errors
when mocking a static method. I describe both errors here (as they are
related).

When run the testcode below I get errors in both files:
First error (in class TestDutFirst):
My tested method works with mocking, however, I get an error as a
second test:
Test mechanism, ClassNotFoundException:
org.mockito.internal.progress.ThreadSafeMockingProgress

Second error (in class TestDutSecond):
This class under test uses the same static method as the class under
test in TestDutFirst
Tests are not run, I get an initializationError
ClassNotFoundException:
org.mockito.internal.progress.ThreadSafeMockingProgress

If I comment out all mocking in class TestDutFirst, the second test
class will have the same behaviour as TestDurFirst with mocking.


My questions are:
- how do I avoid the 'Test mechanism' error?
- to get the second test to work, do I need to 'unmock' the static
class?

/Mikael


Used jar versions:
junit 4.8.2
mockito-all 1.8.5
powermock-mockito 1.4.8
objenesis 1.2
javassist 3.14.0-GA
cglib-nodep 2.2


build.xml:
<project name="powermocktest" default="all" basedir=".">
<property name="jars.path" value="./jars" />
<property name="develop.path" value="${jars.path}/junit-4.8.2.jar:$
{jars.path}/mockito-all-1.8.5.jar:${jars.path}/powermock-mockito-1.4.8-
full.jar:${jars.path}/objenesis-1.2.jar:${jars.path}/javassist-3.14.0-
GA.jar:${jars.path}/cglib-nodep-2.2.jar" />
<property name="build.dir" value="./build" />
<property name="class.dir" value="${build.dir}/class" />
<property name="unittest.dir" value="${build.dir}/unittest" />
<property name="unittest.doc.dir" value="${build.dir}/unittest/doc" /
>
<property name="unittest.out.dir" value="${build.dir}/unittest/out" /
>
<property name="src.dir" value="./src" />

<target name="init" >
<mkdir dir="${class.dir}" />
<mkdir dir="${unittest.dir}" />
<mkdir dir="${unittest.out.dir}" />
<mkdir dir="${unittest.doc.dir}" />
</target>

<target name="clean" description="cleans all built classes and
jars">
<delete dir="${build.dir}" includeemptydirs="true" />
</target>

<target name="compile" description="compile classes" depends="init">
<javac srcdir="${src.dir}" destdir="${class.dir}"
failonerror="true"
classpath="${develop.path}" debug="true"
debuglevel="lines,vars,source"
includeAntRuntime="false" />
</target>

<target name="test" description="run unit tests" depends="init">
<junit printsummary="true" failureproperty="junit.failure">
<classpath path="${develop.path}:${class.dir}" />
<batchtest todir="${unittest.out.dir}" >
<fileset dir="${src.dir}" >
<include name="**/*Test*.java" />
</fileset>
<formatter type="xml" />
</batchtest>
</junit>

<junitreport todir="${unittest.doc.dir}" >
<fileset dir="${unittest.out.dir}" />
<report todir="${unittest.doc.dir}" />
</junitreport>
<fail if="junit.failure" message="Unit test(s) failed. See
reports!" />
</target>

<target name="all" depends="clean,compile,test" />
</project>


Sources are located in the ./src/powermocktest directory.
Class Static.java:
package powermocktest;

public class Static {
public static String getString() {
return "Hello";
}
}


Class DutFirst.java:
package powermocktest;

public class DutFirst {
public String get() {
return Static.getString();
}
}


Class DutSecond.java:
package powermocktest;

public class DutSecond {
public String get() {
return Static.getString();
}
}


Class TestDutFirst.java:
package powermocktest;

import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;


@RunWith (PowerMockRunner.class)
public class TestDutFirst {

@PrepareForTest(Static.class)
@Test
public void testGet() {
PowerMockito.mockStatic(Static.class);
PowerMockito.when(Static.getString()).thenReturn("World");
DutFirst dut = new DutFirst();
assertEquals("Returned word", "World", dut.get());
}
}


Class TestDutSecond.java:
package powermocktest;

import static org.junit.Assert.*;
import org.junit.Test;

import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith (PowerMockRunner.class)
public class TestDutSecond {

@PrepareForTest(Static.class)
@Test
public void testGet() {
PowerMockito.mockStatic(Static.class);
PowerMockito.when(Static.getString()).thenReturn("World");
DutSecond dut = new DutSecond();
assertEquals("Returned word", "World", dut.get());
}
}

Johan Haleby

unread,
Mar 24, 2011, 6:17:08 AM3/24/11
to powe...@googlegroups.com
Hi,

This is a very strange error unless you have an unsupported combination of PowerMock and Mockito in classpath. Can you verify that you really have PowerMock 1.4.8 and Mockito 1.8.5 in classpath?

/Johan
> --
> You received this message because you are subscribed to the Google Groups "PowerMock" group.
> To post to this group, send email to powe...@googlegroups.com.
> To unsubscribe from this group, send email to powermock+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/powermock?hl=en.
>
>

Mikael

unread,
Mar 24, 2011, 7:02:09 AM3/24/11
to PowerMock


On 24 mar, 11:17, Johan Haleby <johan.hal...@gmail.com> wrote:
Hello

Thank you for answering.

I have no environment classpath specified. The only classpath used is
the one set in the build script.

Environment is Linux (Centos kernel 2.6.18) with ant 1.8.0RC1 and java
1.6.0_17.
I made a test in Windows XP, ant version 1.8.1, java 1.6.0_16 with the
same result, apart from that the windows unit test report also shows a
stacktrace (see below).

/Mikael


Stacktrace from TestDutFirst:
java.lang.RuntimeException: java.lang.ClassNotFoundException:
org.mockito.internal.progress.ThreadSafeMockingProgress
at
org.powermock.api.support.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:
52)
at
org.powermock.api.support.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:
26)
at org.powermock.api.mockito.internal.mockcreation.MockCreator
$MockitoStateCleaner.clearThreadLocalIn(MockCreator.java:157)
at org.powermock.api.mockito.internal.mockcreation.MockCreator
$MockitoStateCleaner.clearMockProgress(MockCreator.java:148)
at org.powermock.api.mockito.internal.mockcreation.MockCreator
$MockitoStateCleaner.run(MockCreator.java:143)
at org.powermock.core.MockRepository.clear(MockRepository.java:125)
at
org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:
2042)
at
org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:
913)
at
org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:
887)
at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:466)
at
org.powermock.modules.junit4.common.internal.impl.PowerMockJUnit4RunListener.testFinished(PowerMockJUnit4RunListener.java:
55)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:
222)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:
161)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$1.run(PowerMockJUnit44RunnerDelegateImpl.java:135)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:
133)
at
org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:
112)
at
org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:
57)
Caused by: java.lang.ClassNotFoundException:
org.mockito.internal.progress.ThreadSafeMockingProgress
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at
org.powermock.api.support.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:
50)



Stacktrace from TestDutSecond:
java.lang.RuntimeException: java.lang.ClassNotFoundException:
org.mockito.internal.progress.ThreadSafeMockingProgress
at
org.powermock.api.support.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:
52)
at
org.powermock.api.support.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:
26)
at org.powermock.api.mockito.internal.mockcreation.MockCreator
$MockitoStateCleaner.clearThreadLocalIn(MockCreator.java:157)
at org.powermock.api.mockito.internal.mockcreation.MockCreator
$MockitoStateCleaner.clearMockProgress(MockCreator.java:148)
at org.powermock.api.mockito.internal.mockcreation.MockCreator
$MockitoStateCleaner.run(MockCreator.java:143)
at org.powermock.core.MockRepository.clear(MockRepository.java:125)
at
org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.<init>(AbstractCommonPowerMockRunner.java:
46)
at
org.powermock.modules.junit4.PowerMockRunner.<init>(PowerMockRunner.java:
27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
Caused by: java.lang.ClassNotFoundException:
org.mockito.internal.progress.ThreadSafeMockingProgress
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at
org.powermock.api.support.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:
50)



> Hi,
>
> This is a very strange error *unless* you have an unsupported combination of
> PowerMock and Mockito in classpath. Can you verify that you *really* have

Johan Haleby

unread,
Mar 28, 2011, 2:15:09 AM3/28/11
to powe...@googlegroups.com
It shouldn't matter but could you try moving the @PrepareForTest(Static.class) to the class-level of the test? You shouldn't use the PrepareForTest level at the method level other than in really special cases.

/Johan

Mikael

unread,
Mar 28, 2011, 9:36:01 AM3/28/11
to PowerMock
Hello

Still the same result after moving the PrepareForTest statement.

Do you know of any sample project (including buildfile) to download
that demonstrates the most common usages of PowerMock and Mockito?

/Mikael



On 28 mar, 08:15, Johan Haleby <johan.hal...@gmail.com> wrote:
> It shouldn't matter but could you try moving the @PrepareForTest(Static.class)
> to the class-level of the test? You shouldn't use the PrepareForTest level
> at the method level other than in really special cases.
>
> /Johan
>
> ...
>
> läs mer »

Johan Haleby

unread,
Mar 28, 2011, 10:05:20 AM3/28/11
to powe...@googlegroups.com
I still suspect that it is a class-path issue. Do you get the same result when you run the test from your IDE (without using ant)?

There are several examples in subversion that you can have a look at but they're all using Maven.

/Johan


--

Mikael

unread,
Mar 28, 2011, 11:04:48 AM3/28/11
to PowerMock
You're probably right about the classpath.

The tests run OK in Eclipse.

When I set environment variable CLASSPATH to the jars, the two
testclasse's testmethods pass the test, however there is still the
'Test mechanism' error. This time the Test mechanism error occurs in
both test files.

/Mikael

On 28 mar, 16:05, Johan Haleby <johan.hal...@gmail.com> wrote:
> I still suspect that it is a class-path issue. Do you get the same result
> when you run the test from your IDE (without using ant)?
>
> There are several examples in
> subversion<http://code.google.com/p/powermock/source/browse/#svn/tags/powermock-...>that
> you can have a look at but they're all using Maven.
>
> /Johan
>
> ...
>
> läs mer »

Johan Haleby

unread,
Mar 28, 2011, 11:11:21 AM3/28/11
to powe...@googlegroups.com
I think you have to compare the class-path order you have in Eclipse to the class-path order you have in ant and investigate what the difference is. Perhaps you have two different version of Mockito in classpath and an incorrect version is loaded before version 1.8.5.

/Johan

> ...
>
> läs mer »

Mikael

unread,
Mar 30, 2011, 3:16:28 AM3/30/11
to PowerMock
One small step forward, regarding classpaths.

Classpaths in Eclipse JUnit test:
Class classloader org.powermock.core.classloader.MockClassLoader
System classloader sun.misc.Launcher$AppClassLoader
Classpath (system classloader):
... /build/class/
... /jars/mockito-all-1.8.5.jar
... /jars/cglib-nodep-2.2.jar
... /jars/javassist-3.14.0-GA.jar
... /jars/junit-4.8.2.jar
... /jars/objenesis-1.2.jar
... /jars/powermock-mockito-1.4.8-full.jar
... /eclipse/configuration/org.eclipse.osgi/bundles/188/1/.cp/
... /eclipse/configuration/org.eclipse.osgi/bundles/186/1/.cp/
... /eclipse/configuration/org.eclipse.osgi/bundles/187/1/.cp/


Classpaths when running ant from command line:
Class classloader org.powermock.core.classloader.MockClassLoader
System classloader sun.misc.Launcher$AppClassLoader
Classpath (system classloader):
/opt/apache-ant-1.8.0RC1/lib/ant-launcher.jar


The problem seems to be how to make ant tell JUnit what classpath to
use.
/Mikael

On 28 mar, 17:11, Johan Haleby <johan.hal...@gmail.com> wrote:
> I think you have to compare the class-path order you have in Eclipse to the
> class-path order you have in ant and investigate what the difference is.
> Perhaps you have two different version of Mockito in classpath and an
> incorrect version is loaded before version 1.8.5.
>
> /Johan
>
> ...
>
> läs mer »

Mikael

unread,
Apr 6, 2011, 2:44:48 AM4/6/11
to PowerMock
An ugly workaround to get the jars into the system classpath is to run
the test from java. Running the buildtarget below solves the problem.
However, it runs extermely slow, compared to running the test target
in a normal way.

<target name="uglytest" depends="clean,compile">
<java classname="org.apache.tools.ant.launch.Launcher" fork="true"
dir="${basedir}" failonerror="true">
<classpath path="${ant.home}/lib/ant-launcher.jar:${develop.path}:$
{class.dir}" />
<arg value="test" />
</java>
</target>
> ...
>
> läs mer »

Mikael

unread,
Apr 6, 2011, 2:54:43 AM4/6/11
to PowerMock
I wonder, could this be a problem with the MockClassLoader not loading
the jars?

I made a small testproject using log4j as external jar (and not using
any powermock jars). The used classloaders are:
Class classloader: org.apache.tools.ant.util.SplitClassLoader
SplitClassLoader classpath: /opt/apache-ant-1.8.0RC1/lib/ant-
launcher.jar:/opt/apache-ant-1.8.0RC1/lib/ant.jar:/opt/apache-
ant-1.8.0RC1/lib/ant-junit.jar:/xxx/logtest/jars/log4j-1.2.16.jar:/xxx/
logtest/jars/junit-4.8.2.jar:/xxx/logtest/build/class
System classloader sun.misc.Launcher$AppClassLoader
Classpath: /opt/apache-ant-1.8.0RC1/lib/ant-launcher.jar

So, the classpath set in the test target seems to work and jar paths
are forwarded to the test class' classloader. This seems not to be the
case when I use PowerMock and the test class' classloader is
MockClassLoader.

/Mikael
> ...
>
> läs mer »

Johan Haleby

unread,
Apr 6, 2011, 3:16:12 AM4/6/11
to powe...@googlegroups.com, Mikael
Since it works well for you in Eclipse I assume that it must be some classpath configuration issues. I've used PowerMock with ant before and it has worked well (but we did ran into some issues because the wrong junit version was on classpath).

/Johan

> ...
>
> läs mer »

Mikael

unread,
Apr 6, 2011, 9:08:22 AM4/6/11
to PowerMock
Ok
Could you then please tell me whats wrong in the buildfile above or,
even better, show a sample buildfile that works.

/Mikael

On 6 Apr, 09:16, Johan Haleby <johan.hal...@gmail.com> wrote:
> Since it works well for you in Eclipse I assume that it must be some
> classpath configuration issues. I've used PowerMock with ant before and it
> has worked well (but we did ran into some issues because the wrong junit
> version was on classpath).
>
> /Johan
>
> ...
>
> läs mer »

Johan Haleby

unread,
Apr 6, 2011, 9:16:42 AM4/6/11
to powe...@googlegroups.com, Mikael
Sorry but I don't have a PowerMock+Ant example but people have got it to work (myself included but it was several years ago). There was another guy who had trouble with ant but it turned out he had the same problem with the JUnit version as I had (you can read the thread here).

/Johan

> ...
>
> läs mer »

Reply all
Reply to author
Forward
0 new messages