|
FAQ
Frequently asked questions
java.lang.NoClassDefFoundError: org/junit/internal/runners/BeforeAndAfterRunner orjava.lang.SecurityException: class "org.junit.internal.runners.TestClass"'s signer information does not match signer information of other classes in the same packageexception. What's wrong?You're probably using the wrong PowerMockRunner. There's one runner made for JUnit 4.4 and above and a second runner made for JUnit 4.0-4.3 (although the latter also works for some older minor versions of JUnit 4.4). Try switching from the org.powermock.modules.junit4.PowerMockRunner to org.powermock.modules.junit4.legacy.PowerMockRunner or vice versa. Look at the getting started guide to see how to configure this in maven.
Either: <build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkMode>pertest</forkMode>
</configuration>
</plugin>
</plugins>
</build>The reason is that the XML framework tries to instantiate classes using reflection and does this from the thread context classloader (PowerMock's classloader) but then tries to assign the created object to a field not loaded by the same classloader. When this happens you need to make use of the @PowerMockIgnore annotation to tell PowerMock to defer the loading of a certain package to the system classloader. What you need to ignore is case specific but usually it's the XML framework or some packages that interact with it. E.g. @PowerMockIgnore({"org.xml.*", "javax.xml.*"}). Another option would be to try to bootstrap using our Java Agent. This is because they're loaded by Java's bootstrap classloader and cannot be byte-code manipulated by PowerMock's classloader. Since PowerMock 1.2.5 there's a work-around, please have a look at this simple example to see how it's done. java.lang.ClassCastException: org.hibernate.ejb.HibernatePersistence cannot be cast to javax.persistence.spi.PersistenceProvider
at javax.persistence.Persistence.findAllProviders(Persistence.java:80)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:49)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
...Solution: Upgrade to PowerMock 1.3+ or use @PowerMockIgnore("javax.persistence.*") at the class-level of your test. log4j:ERROR A "org.apache.log4j.xml.DOMConfigurator" object is not assignable to a "org.apache.log4j.spi.Configurator" variable. log4j:ERROR The class "org.apache.log4j.spi.Configurator" was loaded by log4j:ERROR [org.powermock.core.classloader.MockClassLoader@14a55f2] whereas object of type log4j:ERROR "org.apache.log4j.xml.DOMConfigurator" was loaded by [sun.misc.Launcher$AppClassLoader@92e78c]. log4j:ERROR Could not instantiate configurator [org.apache.log4j.xml.DOMConfigurator]. orCaused by: org.apache.commons.logging.LogConfigurationException: Invalid class loader hierarchy. You have more than one version of 'org.apache.commons.logging.Log' visible, which is not allowed. There are a couple of different solutions to this:
Yes, since version 1.3.5 PowerMock does have basic TestNG support. No. PowerMock extends other mock frameworks such as EasyMock with powerful capabilities such as static mocking. Yes, you can make use of the PowerMockRule. |
► Sign in to add a comment
I am trying to use a PowerMock? class along with Fitenesse, although the code executes as expected when executed as a Junit, when used via fitnesse the original static methods which is mocked @SuppressStaticInitializerFor? is been called, any advice ?
Please send your question to the mailing-list by joining our google group.
I get: Could not create and run test suite: java.lang.ClassCastException?: org.powermock.api.easymock.mockpolicies.Log4jMockPolicy? when trying to use this mock policy to suppress log4j errors
Try to use @PowerMockIgnore?(org.apache.log4j") instead
Hi,
org.omg.CORBA.INITIALIZE: can't instantiate default ORB implementation org.jacorb.orb.ORB vmcid: 0x0 minor code: 0 completed: No
... Caused by: java.lang.ClassCastException?: org.jacorb.orb.ORB cannot be cast to org.omg.CORBA.ORBWhat can you recommend me?
Please join and use our mailing-list (powermock at googlegroups.com) instead of posting questions here.
hi, am new to powermock. when i tried using it am getting the below error. any suggestion would be great.
java.lang.LinkageError?: Class org/easymock/IMocksControl violates loader constraints
Please post an example on the mailing-list (powermock at googlegroups.com).
For #4 I find that I need to wildcard the package name like so :
@PowerMockIgnore( { "org.apache.commons.logging.*", "org.apache.log4j.*" })That's correct, thanks for spotting it.
hi ,
new FindProgramHelper?().find(settop, criteria, null);
like this PowerMock?.createMockAndExpectNew(FindProgramHelper?.class); EasyMock.expect(findPgm.find(p1,p2,p3)).andReturn(value); PowerMock?.replay(FindProgramHelper?.class);
but they using two different constructor for calling different method of same class like this
int value = new FindProgramHelper?().find(p1,p2,p3); int v2 = new FindProgramHelper?().findScheduleTimes(p1,p2);
so i try this way
PowerMock?.createMockAndExpectNew(FindProgramHelper?.class); EasyMock.expectLastCall().anyTimes();
PowerMock?.replay(FindProgramHelper?.class);but it showing error
java.lang.AssertionError?:
please tell me
hi,
My junit test class MyclassTest?.java having 20 testcase1().. methods
i execute the one testcase1() just select and right click to execute that.
But i try to execute the only 5 test method instead of 20,it is not getting executed .how can i execute that (without @ignore option in other method)
using eclipse
Please use the mailing list instead of asking questions here.
Do you have plans to support JMock?
>> I cannot mock classes in from java.lang, java.net, java.io or other system classes, why?
Is this outdated? I've successfully mocked java.net.URI and java.net.URL objects.
leonardovarela: It would be really cool and I've thought about it but I just don't have time to do it. But if you want to mock final classes in JMock today you can still use PowerMock? for it. Just depend on powermock-core and prepare the final class for test.
jrh3k5: It works, but you need to prepare the class calling the URL object, not the URL object itself.
Hi I am trying to unit test a servlet using powermock easymock , but getting an exception in the createmock of ServletRequest? java.lang.ClassFormatError?: Absent Code attribute in method that is not native or abstract in class file javax/servlet/http/Cookie
@PrepareForTest?(IdGenerator?.class) @RunWith?(PowerMockRunner?.class) public class TestSipServlet?
{
}
Hi, there is another possible solution for the Log4J error. Add -Dlog4j.ignoreTCL=true as VM-Arguments (also possible with ant vmargs) to your test-run-configuration. this will stop log4j that something is wrong, and will output all logging as expected (note: this is the log4j autoconfiguration, the console is possible not the same as you expect from your log4j.properties configuration, but enough for testing)
Hope this helps ;) and someone add's this to the FAQ.
Is there any possibility to mock only one public static method for public class (instances of this class can not be created!).
I have 'Helper' class with private constructor to ommit creation any instances of it. This class have many public static methods, but during the unit test I want to mock only one static method 'public static int make(String str, int a)' to return '10', but rest of the methods (about 23 methods) I want work normally 'CallRealMethod?'. I work with Mockito library.
Is there any simple possibility to obtain such functionality?
Is it possible to use loggers in the test class itself?
e.g.:
public class MyTest { private static Logger logger = Logger.getLogger(MyTest.class);If I use any of the available solutions for
When running a PowerMock?? test log4j gives me the following (or something similar) error, what now?
I don't get the logging error mentioned there, but I cannot have log entrances in my test clasess.
Any ideas? Regards, Nico