gordonad / powermock

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

JUnit launches unexpected "null" RuntimeException during expectPrivate of a native method #78

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi, I'm trying to use powermock to test a singleton class using a native 
function, and get recurrent error from JUnit launcher.

Basically, I've 3 classes:
-MyClass is a singleton, and contains a native method as well as a non-
native one; the native method throws
-MyClassException
-MyClassTest

I used JUnit before, but I'm a bit new wrt mocks... so I may have made 
a stupid noob mistake... :\

What steps will reproduce the problem?
1. javac -cp . MyClass.java MyExceptionClass.java
2. javac -cp .;junit-4.5.jar;powermock-1.0-
full.jar;easymock-2.4.jar;easymockclassextension-2.4.jar;javassist-3.9.0.GA.jar;
cglib-2.2.jar;asm-3.1.jar 
MyClassTest.java
3. java -cp .;junit-4.5.jar;powermock-1.0-
full.jar;easymock-2.4.jar;easymockclassextension-2.4.jar;javassist-3.9.0.GA.jar;
cglib-2.2.jar;asm-3.1.jar 
org.junit.runner.JUnitCore MyClassTest

What is the expected output? What do you see instead?
I get the following stack trace:
JUnit version 4.5
.E
Time: 0,921
There was 1 failure:
1) null
java.lang.RuntimeException: test should never throw an exception to this 
level
    at 
org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters
(MethodRoadie.java:97)
    at org.junit.internal.runners.MethodRoadie.runTest
(MethodRoadie.java:84)
    at org.junit.internal.runners.MethodRoadie.run
(MethodRoadie.java:49)
    at 
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.in
vokeTestMethod
(PowerMockJUnit44RunnerDelegateImpl.java:200)
    at 
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.ru
nMethods
(PowerMockJUnit44RunnerDelegateImpl.java:150)
    at 
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$1.run(PowerMockJUnit44RunnerDelegateImpl.java:124)
    at org.junit.internal.runners.ClassRoadie.runUnprotected
(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected
(ClassRoadie.java:44)
    at 
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.ru
n
(PowerMockJUnit44RunnerDelegateImpl.java:122)
    at 
org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run
(JUnit4TestSuiteChunkerImpl.java:89)
    at 
org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.
run
(AbstractCommonPowerMockRunner.java:44)
    at org.junit.runners.Suite.runChild(Suite.java:115)
    at org.junit.runners.Suite.runChild(Suite.java:23)
    at org.junit.runners.ParentRunner.runChildren
(ParentRunner.java:180)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
    at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
    at org.junit.internal.runners.statements.RunBefores.evaluate
(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate
(RunAfters.java:31)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:116)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:107)
    at org.junit.runner.JUnitCore.runMain(JUnitCore.java:88)
    at org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:54)
    at org.junit.runner.JUnitCore.main(JUnitCore.java:46)

FAILURES!!!
Tests run: 1,  Failures: 1

What version of the product are you using? On what operating system?
SUN javac 1.5.0_16
SUN java 1.6.0_10-b33

Thanks in advance for your help.

Frédéric Delanoy

Original issue reported on code.google.com by frederic.delanoy@gmail.com on 29 Nov 2008 at 3:10

Attachments:

GoogleCodeExporter commented 9 years ago
Please join our googlegroup and post questions there instead of filing a new 
issue.

Original comment by johan.ha...@gmail.com on 2 Dec 2008 at 12:01

GoogleCodeExporter commented 9 years ago
I cannot get the error you're describing when I run it in Eclipse, how ever you 
have
a small error in your test:
expectPrivate(retrMock, jniMeth, "param value").once().andStubReturn("result");
should be
expectPrivate(retrMock, jniMeth, "param value").andStubReturn("result");

The whole test can also be rewritten as this:
@RunWith(PowerMockRunner.class)
// don't load JNI lib; simulate
@SuppressStaticInitializationFor( { "MyClass" }) 
public class MyClassTest {

    @Test
    public void testMy_methode() throws Exception {
        MyClass retrMock = createPartialMock(MyClass.class, "my_native_method");

        // Fails after following line...
        expectPrivate(retrMock, "my_native_method", "param value").andStubReturn("result");
        replayAll();

        String res = retrMock.my_method("param value");

        verifyAll();

        assertEquals("result", res);
    }
}

I'll try from the command-line next.

Original comment by johan.ha...@gmail.com on 2 Dec 2008 at 12:18

GoogleCodeExporter commented 9 years ago
This is also the reason why your code fails in command-line. 

java -cp
.;cglib-nodep-2.1_3.jar;easymock-2.4.jar;easymockclassextension-2.4.jar;junit-4.
5.jar;powermock-1.0-full.jar;javassist-
3.8.0.GA.jar org.junit.runner.JUnitCore MyClassTest

works when you remove the once() call. once() can only be used for void 
methods. I
don't know why the error message is not reported in command-line though. Also 
please
use the dependencies from the powermock-with-dependencies zip file (though I 
think it
should work with the dependencies that you specified as well).

And please you the mailing list the next time. Good luck.

Original comment by johan.ha...@gmail.com on 2 Dec 2008 at 12:30

GoogleCodeExporter commented 9 years ago
OK, 

Thanks a lot for your help.
Sorry for the newsgroup I hadn't seen/used... I should have drunken more 
caffeine...

Anyway great stuff you've made!

Original comment by frederic.delanoy@gmail.com on 3 Dec 2008 at 12:07