tiebin-zhang / powermock

Automatically exported from code.google.com/p/powermock
Apache License 2.0
0 stars 0 forks source link

Test case with expectedExceptions fails when using PowerMock with TestNG. #366

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create a Test class which has the below method 
@ObjectFactory
    public IObjectFactory getObjectFactory() {
        return new org.powermock.modules.testng.PowerMockObjectFactory();
    }
2. Create a test case with expectedExceptions annotation 
3. Run it using Eclipse TestNG plugin.

What is the expected output? What do you see instead?
We are expecting the test case to pass but test case fails and gives the below 
message . 
Expected exception com.org.test.DAOException but got org.testng.TestException: 
Expected exception com.org.test.DAOException but got com.org.test.DAOException: 
com.org.test.DAOException
    at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1459)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1222)

What version of the product are you using? On what operating system?
TestiNG 6.3.1
Power mock 1.4.11.
Eclipse Plugin for TestNG.
Windows XP.

Please provide any additional information below.

The same test case passes with TestNG when we don't use power mock. 

After debugging through the TestNG code, noticed that below line (#  1794) in 
isExpectedException method 
inside the testng/src/main/java/org/testng/internal/Invoker.java fails.

exception.isAssignableFrom(realExceptionClass):  this statement always returns 
false even if the exception and realExpectionClass are same.

Original issue reported on code.google.com by archanar...@gmail.com on 2 Feb 2012 at 7:37

GoogleCodeExporter commented 9 years ago
Hi, 

Could you debug and see which classloader loaded the "exception" and which 
classloader that loads the "realExceptionClass". Is it the same classloader 
instance?

Original comment by johan.ha...@gmail.com on 3 Feb 2012 at 9:09

GoogleCodeExporter commented 9 years ago
I noticed that classloaders are different.

exception is loaded by sun.misc.Launcher$AppClassLoader@1ba34f2

realExceptionClass is loaded by 
org.powermock.core.classloader.MockClassLoader@121b59a

Original comment by archanar...@gmail.com on 3 Feb 2012 at 6:49

GoogleCodeExporter commented 9 years ago
Ok that's what I suspected. Thanks for reporting. As a work-around try to 
ignore the exception using @PowerMockIgnore and see if that helps.

Original comment by johan.ha...@gmail.com on 7 Feb 2012 at 10:31

GoogleCodeExporter commented 9 years ago
@PowerMockIgnore is not ignoring the exception. This does not help me in 
passing the test case.

Original comment by archanar...@gmail.com on 28 Feb 2012 at 2:11

GoogleCodeExporter commented 9 years ago
Any thoughts on how we could work around this?

Original comment by bugt...@gmail.com on 22 Aug 2012 at 3:17

GoogleCodeExporter commented 9 years ago
Same problem using:
testng-6.8.jar
powermock-mockito-1.4.12-full.jar

Original comment by Thomas.M...@gmail.com on 14 Oct 2012 at 8:32

GoogleCodeExporter commented 9 years ago
and here is some example code https://gist.github.com/a3f464eb614a4a104482

And to be more concrete:
Exceptions from the jdk (and other external libs?) will correct catched but own 
exceptions will not.

Original comment by Thomas.M...@gmail.com on 14 Oct 2012 at 9:39

GoogleCodeExporter commented 9 years ago
You can try extending from PowerMockTestCase and see if that works any better.

Original comment by johan.ha...@gmail.com on 14 Oct 2012 at 12:49

GoogleCodeExporter commented 9 years ago
Did not help in my case.

Original comment by majorpe...@gmail.com on 14 Oct 2012 at 1:33

GoogleCodeExporter commented 9 years ago
Did not change any thing in my case.

Also adding 
object-factory="org.powermock.modules.testng.PowerMockObjectFactory" to testNG 
suite.xml did not help.

Original comment by Thomas.M...@gmail.com on 16 Oct 2012 at 3:37

GoogleCodeExporter commented 9 years ago
Alright, thanks for trying out. I'd really appreciate if anyone could 
investigate a bit more in detail why this happens (and preferably create a 
patch as well :)). 

Original comment by johan.ha...@gmail.com on 16 Oct 2012 at 4:09

GoogleCodeExporter commented 9 years ago
The workaround I've been using for couple of more powermock tests. Have two 
test classes one with powermock and one without powermock. This allows me to 
avoid powemock issues temporarily.
e.g.
MyBeanTest.java
MyBeanStaticTest.java

Original comment by kishore....@gmail.com on 21 Jan 2013 at 4:07

GoogleCodeExporter commented 9 years ago
Can anyone tell how I, in Eclipse ( or any other 3rd party tool), can see which 
classloader is used for each class?

//mike

Original comment by mikaelpe...@gmail.com on 20 Dec 2013 at 3:15

GoogleCodeExporter commented 9 years ago
Since there was absolutely no progress on this quite gamebreaking issue for 
almost two years now, here's a patch I just came up with after a days worth of 
investigation... Feel free to use it at your own risk..

Original comment by majorpe...@gmail.com on 21 Dec 2013 at 8:30

Attachments:

GoogleCodeExporter commented 9 years ago
Hahahahah THANK YOU!

Original comment by bugt...@gmail.com on 21 Dec 2013 at 8:53

GoogleCodeExporter commented 9 years ago
Looks good to me so I will try to verify it.

//mike

Original comment by mikaelpe...@gmail.com on 23 Dec 2013 at 6:52

GoogleCodeExporter commented 9 years ago
I am trying to build 1.5.3-SNAPSHOT with this fix but I get the following error:

ERROR] Failed to execute goal 
org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) 
on project powermock-api-mockito: Compilation failure
[ERROR] 
/mypath_to_home/powermock/api/mockito/src/main/java/org/powermock/api/mockito/in
ternal/expectation/DelegatingToConstructorsOngoingStubbing.java:[115,31] type 
parameters of <M>M cannot be determined; no unique maximal instance exists for 
type variable M with upper bounds M,java.lang.Object
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e 
switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please 
read the following articles:
[ERROR] [Help 1] 
http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Any ideas how I can fix it?

//mike

Original comment by mikaelpe...@gmail.com on 23 Dec 2013 at 9:28

GoogleCodeExporter commented 9 years ago
This sounds like:
http://stackoverflow.com/questions/314572/bug-in-eclipse-compiler-or-in-javac-ty
pe-parameters-of-t-cannot-be-determined
FWIW building trunk with JDK7 indeed works. My patch was created against the 
1.5.2 tag by the way.

Original comment by majorpe...@gmail.com on 23 Dec 2013 at 9:58

GoogleCodeExporter commented 9 years ago
I am running 1.5.3-SNAPSHOT since I guess that is where it will be added 
or?Swithing to 1.7 helped to get by the error metioned but I got stuck with:

[ERROR] Failed to execute goal 
org.apache.maven.plugins:maven-assembly-plugin:2.2-beta-5:attached 
(make-assembly) on project powermock-release-junit-without-mock-framework: 
Failed to create assembly: Error adding file-set for 
'org.powermock:powermock-release-without-test-and-mock-framework:jar:1.5.3-SNAPS
HOT' to archive: Expected 
org.codehaus.plexus.components.io.resources.PlexusIoArchivedResourceCollection, 
got org.codehaus.plexus.components.io.resources.PlexusIoFileResourceCollection 
-> [Help 1]

I need it packed to be able to test in my test project ( maven project).

Any more ideas?
//mike

Original comment by mikaelpe...@gmail.com on 23 Dec 2013 at 1:44

GoogleCodeExporter commented 9 years ago
Don't seem to have the power to run it ....
ERROR] Failed to execute goal 
org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on 
project powermock-classloading-objenesis: Execution default-test of goal 
org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test failed: The forked 
VM terminated without saying properly goodbye. VM crash or System.exit called ? 
-> [Help 1]

I used:

set MAVEN_OPTS=-Xmx2048m

//mike

Ps. you don't happen to have a prefixed jar file of powermock that I can test 
against?

Original comment by mikaelpe...@gmail.com on 23 Dec 2013 at 2:48

GoogleCodeExporter commented 9 years ago
Here's a build I just made - first my build failed with a VM crash at 
objenesis, but afterwards the build succeeded with:
mvn clean install -rf :powermock-junit4-rule-objenesis-test
I've used MAVEN_OPTS="-Xmx1g -XX:MaxPermSize=256m".
https://www.dropbox.com/s/x27b20jbp9npw0v/powermock-mockito-testng-1.5.3-SNAPSHO
T.zip

Original comment by majorpe...@gmail.com on 23 Dec 2013 at 3:05

GoogleCodeExporter commented 9 years ago
I've applied the patch to trunk now, thanks!

To build PowerMock you must use JDK 6 otherwise you'll likely run into VM 
crashes.

Original comment by johan.ha...@gmail.com on 25 Dec 2013 at 3:05

GoogleCodeExporter commented 9 years ago
When will there be a new delivery of powermock?

//mike

Original comment by mikaelpe...@gmail.com on 2 Jan 2014 at 8:12

GoogleCodeExporter commented 9 years ago
Any idea how I can make a beta release ( to use in our team) of power-mock to 
see if we have all that we need?

//mike

Original comment by mikaelpe...@gmail.com on 7 Jan 2014 at 9:41

GoogleCodeExporter commented 9 years ago
Well you could build it yourself from trunk using Java 6 (and skip javadoc 
generation) and then use it. Making a release of PowerMock takes me several 
hours so it's not something I enjoy doing. I'm thinking about making a new 
release soon so that you'll get this fix but I would _really_ like someone to 
try it out first so I don't have to make a new release again. This is how I 
(quickly) build PowerMock on my Mac:

JAVA_HOME="/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home" 
mvn clean install -Dmaven.javadoc.skip=true

Original comment by johan.ha...@gmail.com on 7 Jan 2014 at 12:06

GoogleCodeExporter commented 9 years ago
I used jdk1.6 and got the following test to fail:

INFO] powermock-junit4-rule-xstream-test ................ FAILURE [5.155s]

Failed tests:   
testSupressStaticInitializer(org.powermock.modules.test.junit4.rule.xstream.Stat
icInitializerExampleTest): Should be null because the static initializer should 
be suppressed

Is this known? 

I added a @RunWith(PowerMockRunner.class) and it seems to pass. Is it ok?

//mike

//mike

Original comment by mikaelpe...@gmail.com on 9 Jan 2014 at 12:25

GoogleCodeExporter commented 9 years ago
Got the same for:

Failed tests:   
testSupressStaticInitializer(org.powermock.modules.test.junit4.rule.objenesis.St
aticInitializerExampleTest): Should be null because the static initializer 
should be suppressed

I tried @RunWith(PowerMockRunner.class) but then compiler says:

src/test/java/org/powermock/modules/test/junit4/rule/objenesis/StaticInitializer
ExampleTest.java:[16,1] error: cannot find symbol

I will try to run it from command line. What version of maven are you using?

//mike

Original comment by mikaelpe...@gmail.com on 9 Jan 2014 at 2:04

GoogleCodeExporter commented 9 years ago
I'm using Maven 3.1 on MacOSX. Try disabling the test if it doesn't work 
(although it should work).

Original comment by johan.ha...@gmail.com on 9 Jan 2014 at 6:00

GoogleCodeExporter commented 9 years ago
Yes selected a newer version of jdk1.6 ( 1.6.0_45) and used ( on SuSE linux 64 
bit):

env MAVEN_OPTS="-Xmx1g -XX:MaxPermSize=256m" mvn clean install 
-Dmaven.javadoc.skip=true

And it worked nice.

//mike

Original comment by mikaelpe...@gmail.com on 10 Jan 2014 at 7:49

GoogleCodeExporter commented 9 years ago
Great! Then I'll release a new version right away. It'll hopefully be available 
within a few hours.

Original comment by johan.ha...@gmail.com on 10 Jan 2014 at 8:38

GoogleCodeExporter commented 9 years ago
It's now released in Maven. I'll update the documentation and upload the 
binaries etc later.

Original comment by johan.ha...@gmail.com on 10 Jan 2014 at 9:38