I quite often get the question if PowerMock can be used together with Spring integration testing or other frameworks that require a JUnit runner to bootstrap. The answer up until now has been somewhat ambiguous. The reason is that for the last year or so we’ve been working with a new way of bootstrapping PowerMock that uses a JUnit Rule instead of a JUnit runner. Implementing this rule proved to be quite hard since it required deep-cloning of an entire object graph into another classloader (more info about this here). For example we ran into very strange problems that caused the JVM to crash on some environments while it worked in others. In the new release of PowerMock 1.4.7 we refactored the classloader execution from the deep-cloning and made the deep-cloning implementations pluggable. This means that we now have an option to use X-Stream as deepcloner which proves to be more robust than our Objenesis implementation (albeit not as fast, not as precise and not as cool :)).
Naive example
Let’s say we have a very simple Spring bean like this:
@Component
public class MyBean {
public Message generateMessage() {
final long id = IdGenerator.generateNewId();
return new Message(id, "My bean message");
}
}
and we want to test the generateMessage
method in an integration test. In this test we assume that it’s really important for us to mock the static call to IdGenerator.generateNewId()
. But in order to run the test as a Spring integration test we need to use the SpringJUnit4ClassRunner
which prevents us from using the PowerMockRunner
. So by taking advantage of the PowerMockRule
we can still benefit from PowerMock’s functionality:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/example-context.xml")
@PrepareForTest(IdGenerator.class)
public class SpringExampleTest {
@Rule
public PowerMockRule rule = new PowerMockRule();
@Autowired
private MyBean myBean;
@Test
public void mockStaticMethod() throws Exception {
// Given
final long expectedId = 2L;
mockStatic(IdGenerator.class);
when(IdGenerator.generateNewId()).thenReturn(expectedId);
// When
final Message message = myBean.generateMessage();
// Then
assertEquals(expectedId, message.getId());
assertEquals("My bean message", message.getContent());
}
If you’re used to PowerMock you can see that the only difference between this test and a standard PowerMock test is that we use the PowerMockRule
instead of the PowerMockRunner. You can find the complete example in our SVN repo.
Limitations
For trivial tests like this the Objenesis deep-cloner works just as well as the X-Stream version but in more complex situations X-Stream is more robust. There may still be bugs lurking around and we’re thankful for contributions. For example it’s not possible to mock system classes using any of the implementations yet.
Conclusion
We’ll continue to work on the Objenesis deep-cloner but until it’s working better you can use the X-Stream version to combine PowerMock with frameworks that require a JUnit runner. Please refer to this page to get started with the rule.
Pingback: itemprop="name">Revue de presse, semaine 52 - Espace de fouille
Pingback: itemprop="name">Tweets that mention Using PowerMock with Spring integration testing — Jayway Team Blog -- Topsy.com
Whenever I try and use PowerMock to mock a static class, running with the @RunWith(SpringJUnit4ClassRunner.class) annotation I always get a java.util.ConcurrentModificationException in java.util.Hashtable generated by xStream trying to DeepCloner.clone() an object (unspecified). I’ve tried the objensis route as well but to no avail. Any clues?
Hi,
I’ve never seen it myself but it’s very likely that you need to ignore (using @PowerMockIgnore) some classes when you combine PowerMock and Spring integration testing. Please join our mailing list if you want to discuss it further. It may require you to create a small example demonstrating the issue though.
/Johan
Pingback: itemprop="name">PowerMock for Integration Testing | Jayway Team Blog - Sharing Experience
Hi,
I’m trying to use PowerMockRule as demonstrated above. My class extends a base class that has:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration( locations = {“classpath:resources/spring/applicationContext.xml” } )
@TransactionConfiguration
@Transactional
@Ignore
My class is annotated with @PrepareForTest(ProcessExecutionLog.class), and has the following code:
public void setupExecutionLogManager() {
myProcessExecutionLog = mock(ProcessExecutionLog.class);
PowerMockito.mockStatic(ProcessExecutionLog.class); PowerMockito.when(ProcessExecutionLog.getInstance()).thenReturn(myProcessExecutionLog);
}
I’m getting the following error:
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be ‘a method call on a mock’.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
2. inside when() you don’t call method on mock but on some other object.
at org.powermock.api.mockito.PowerMockito.when(PowerMockito.java:490)
Any idea?
Thanks in advance,
Omri
Surefire 2.2 does not support JUnit 4. I don’t rembeemr exactly, but I think that JUnit 4 test cases (with the @Test annotation) are ignored in 2.2.BTW 2.3 is now final, so you don’t have to use a snapshot from the snapshots repository.
I forgot to mention that the error is thrown for
PowerMockito.when(ProcessExecutionLog.getInstance()).thenReturn(myProcessExecutionLog);
I’m trying to use Powermock in combination with the SpringJUnit4ClassRunner, but when i try to run my test, I get a “Cannot deserialize BeanFactory with id test_context: no factory registered for this id” error. The beanfactory is an XmlWebApplicationContext that are initiated in a custom TestContextLoader. Has any of you guys seen this problem before? And sorry for posting in this forum :)
@Sonni
Similar thing happened to me. I had a failure saying:
“java.io.FileNotFoundException: class path resource [springApplicationContext.xml] cannot be opened because it does not exist”
The class path resource is working when I use Spring and JUnit only, though. weird. seems to me like a bug.
Cheers,
Deksa
I am not able to run my test case (which involves mocking of static method) with Powermockrule using 1.4.10 version of powermock, with easymock and using SpringJUnit4ClassRunner.
The example given above is based on mockito. Does this work with easymock.
Can you please share an example for easymock. It will be very helpful.
Thanks in advance
Kaushal
I’ve downloaded, built and run the examples from SVN and all work okay. On changing the version of Spring to 3.1.1-RELEASE and get the following error:
com.thoughtworks.xstream.converters.ConversionException: Could not call java.util.concurrent.ConcurrentHashMap.readObject() : javassist.NotFoundException: $Proxy4 : javassist.NotFoundException: $Proxy4
—- Debugging information —-
message : javassist.NotFoundException: $Proxy4
cause-exception : java.lang.RuntimeException
cause-message : javassist.NotFoundException: $Proxy4
class : java.lang.Class
required-type : java.lang.Class
converter-type : com.thoughtworks.xstream.converters.SingleValueConverterWrapper
wrapped-converter : com.thoughtworks.xstream.converters.extended.JavaClassConverter
path : /org.powermock.modules.junit4.rule.PowerMockStatement$1/outer-class/fNext/next/testContextManager/testContext/contextCache/contextMap/java.util.concurrent.ConcurrentHashMap/org.springframework.context.support.GenericApplicationContext/classLoader/classes/java-class[49]
line number : 894
class[1] : java.util.Vector
converter-type[1] : com.thoughtworks.xstream.converters.collections.CollectionConverter
class[2] : sun.misc.Launcher$AppClassLoader
converter-type[2] : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
class[3] : org.springframework.context.support.GenericApplicationContext
——————————-
message : Could not call java.util.concurrent.ConcurrentHashMap.readObject()
cause-exception : com.thoughtworks.xstream.converters.ConversionException
cause-message : javassist.NotFoundException: $Proxy4 : javassist.NotFoundException: $Proxy4
class : java.util.concurrent.ConcurrentHashMap
required-type : java.lang.Class
converter-type : com.thoughtworks.xstream.converters.reflection.SerializableConverter
path : /org.powermock.modules.junit4.rule.PowerMockStatement$1/outer-class/fNext/next/testContextManager/testContext/contextCache/contextMap/java.util.concurrent.ConcurrentHashMap/org.springframework.context.support.GenericApplicationContext/classLoader/classes/java-class[49]
line number : 894
class[1] : org.springframework.test.context.ContextCache
converter-type[1] : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
class[2] : org.springframework.test.context.TestContext
class[3] : org.springframework.test.context.TestContextManager
class[4] : org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks
class[5] : org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks
class[6] : org.powermock.modules.junit4.rule.PowerMockStatement
class[7] : org.powermock.modules.junit4.rule.PowerMockStatement$1
version : null
——————————-
at com.thoughtworks.xstream.converters.reflection.SerializationMethodInvoker.callReadObject(SerializationMethodInvoker.java:119)
at com.thoughtworks.xstream.converters.reflection.SerializableConverter.doUnmarshal(SerializableConverter.java:411)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1052)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1036)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:912)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:903)
at org.powermock.classloading.DeepCloner.clone(DeepCloner.java:54)
at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:89)
at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)
at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
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: com.thoughtworks.xstream.converters.ConversionException: javassist.NotFoundException: $Proxy4 : javassist.NotFoundException: $Proxy4
—- Debugging information —-
message : javassist.NotFoundException: $Proxy4
cause-exception : java.lang.RuntimeException
cause-message : javassist.NotFoundException: $Proxy4
class : java.lang.Class
required-type : java.lang.Class
converter-type : com.thoughtworks.xstream.converters.SingleValueConverterWrapper
wrapped-converter : com.thoughtworks.xstream.converters.extended.JavaClassConverter
path : /org.powermock.modules.junit4.rule.PowerMockStatement$1/outer-class/fNext/next/testContextManager/testContext/contextCache/contextMap/java.util.concurrent.ConcurrentHashMap/org.springframework.context.support.GenericApplicationContext/classLoader/classes/java-class[49]
line number : 894
class[1] : java.util.Vector
converter-type[1] : com.thoughtworks.xstream.converters.collections.CollectionConverter
class[2] : sun.misc.Launcher$AppClassLoader
converter-type[2] : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
class[3] : org.springframework.context.support.GenericApplicationContext
——————————-
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readItem(AbstractCollectionConverter.java:71)
at com.thoughtworks.xstream.converters.collections.CollectionConverter.addCurrentElementToCollection(CollectionConverter.java:79)
at com.thoughtworks.xstream.converters.collections.CollectionConverter.populateCollection(CollectionConverter.java:72)
at com.thoughtworks.xstream.converters.collections.CollectionConverter.populateCollection(CollectionConverter.java:66)
at com.thoughtworks.xstream.converters.collections.CollectionConverter.unmarshal(CollectionConverter.java:61)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
at com.thoughtworks.xstream.converters.reflection.SerializableConverter$2.readFromStream(SerializableConverter.java:297)
at com.thoughtworks.xstream.core.util.CustomObjectInputStream.readObjectOverride(CustomObjectInputStream.java:104)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:344)
at java.util.concurrent.ConcurrentHashMap.readObject(ConcurrentHashMap.java:1275)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.thoughtworks.xstream.converters.reflection.SerializationMethodInvoker.callReadObject(SerializationMethodInvoker.java:113)
… 76 more
Caused by: java.lang.RuntimeException: javassist.NotFoundException: $Proxy4
at org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java:187)
at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:147)
at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:67)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at com.thoughtworks.xstream.core.util.ClassLoaderReference.loadClass(ClassLoaderReference.java:31)
at com.thoughtworks.xstream.converters.extended.JavaClassConverter.loadClass(JavaClassConverter.java:67)
at com.thoughtworks.xstream.converters.extended.JavaClassConverter.fromString(JavaClassConverter.java:44)
at com.thoughtworks.xstream.converters.SingleValueConverterWrapper.fromString(SingleValueConverterWrapper.java:41)
at com.thoughtworks.xstream.converters.SingleValueConverterWrapper.unmarshal(SingleValueConverterWrapper.java:49)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
… 109 more
Caused by: javassist.NotFoundException: $Proxy4
at javassist.ClassPool.get(ClassPool.java:450)
at org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java:180)
… 118 more
I then changed thae PowerMock version to the latest 1.4.12 and still got the same error.
Any ideas?
Will this be updated to supported the latest version(s) of Spring?
Thanks
Hi!
Nice post :)
I was curious why did you want to use PowerMock for this example? Wouldn’t it be much easier and clearer to extract the
IdGenerator.generateNewId();
method to some IdFactory bean (through an interface) that would be autowired to your MyBean component. Then either by Spring itself http://toomuchcoding.blogspot.com/2013/08/injecting-test-doubles-in-spring-using.html or Springockito https://bitbucket.org/kubek2k/springockito/wiki/Home you could inject a mock. You could even create your custom fake implementation of the aforementioned interface that would have the @Primary annotation and would get injected to your bean. I think that there are many other possibilities instead of using PowerMock.
Cheers :)
Hi Marcin,
The example presented in the blog is merely to present the syntactics of PowerMock and Spring integration testing. In most cases it’s better not to use PowerMock and refactoring would certainly be a much better solution for this trivial example. Finding good and concise examples for when to use PowerMock is hard.
Regards,
/Johan
Hi, i used PowerMock,but throw an exception:
Caused by: java.lang.IllegalStateException: PowerMockRule can only be used with the system classloader but was loaded by java.net.URLClassLoader@4bcc946b
at org.powermock.modules.junit4.rule.PowerMockRule.(PowerMockRule.java:28)
How can i fix it.
Thanks!