tiebin-zhang / powermock

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

PowerMock does not ignore JUnit Tests with assumeTrue(false) #408

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. create the following class

@RunWith(PowerMockRunner.class)
public class MyJunitTest {

   private static boolean condition = false;

   @Test
   public void myFirstTest() {
      assumeTrue(condition);
      //my mocking and junit stuff
   }
}

What is the expected output? What do you see instead?
The default JUnit runner treats tests with failing assumptions as ignored. But 
test status should be passed.

Instead the test failes  

What version of the product are you using? On what operating system?
powermock-mockito-1.4.12-full with junit-4.8.2

Please provide any additional information below.
Trace of the output
org.junit.internal.AssumptionViolatedException: got: <false>, expected: is 
<true>
    at org.junit.Assume.assumeThat(Assume.java:70)
    at org.junit.Assume.assumeTrue(Assume.java:39)
    at ......myFirstTest
    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 org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:312)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:296)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:112)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:73)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:284)
    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.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:209)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:148)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
    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.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:102)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:42)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    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)

Original issue reported on code.google.com by funk.up...@gmail.com on 13 Nov 2012 at 11:39

GoogleCodeExporter commented 9 years ago
I'm not familiar with assumeTrue(..) and I've never used it. It would be cool 
if PowerMock could support it though. If you're up for it please help out and 
provide a patch to the PowerMockRunner.

Original comment by johan.ha...@gmail.com on 29 Nov 2012 at 6:45

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Well, I have no patch, but perhaps some information:

The PowerMockJUnit44MethodRunner overrides the method runTestMethod of 
MethodRoadie.
In the original code (junit v4.11) there is an if statement that catches those 
exceptions:
-------------------- 8< ------------------
    protected void runTestMethod() {
        try {
            fTestMethod.invoke(fTest);
            if (fTestMethod.expectsException()) {
                addFailure(new AssertionError("Expected exception: " + fTestMethod.getExpectedException().getName()));
            }
        } catch (InvocationTargetException e) {
            Throwable actual = e.getTargetException();

            *if (actual instanceof AssumptionViolatedException) {
                return;
            }* else if (!fTestMethod.expectsException()) {
                addFailure(actual);
            } else if (fTestMethod.isUnexpected(actual)) {
                String message = "Unexpected exception, expected<" + fTestMethod.getExpectedException().getName() + "> but was<"
                        + actual.getClass().getName() + ">";
                addFailure(new Exception(message, actual));
            }
        } catch (Throwable e) {
            addFailure(e);
        }
    }
-------------------- 8< ------------------

Original comment by openid4...@gmail.com on 12 Jun 2013 at 8:08

GoogleCodeExporter commented 9 years ago
This patch works for me.
(org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl)

powermock 1.5
junit 4.11

Original comment by openid4...@gmail.com on 12 Jun 2013 at 8:59

Attachments:

GoogleCodeExporter commented 9 years ago
Couldn't get the patch to work but I tried to implement it myself. Please try 
it out by building PowerMock from source. Thanks for your help!

Original comment by johan.ha...@gmail.com on 13 Jun 2013 at 6:03

GoogleCodeExporter commented 9 years ago
Well, the exception has gone, but the test case is marked as passed, instead of 
skipped?

Looking into the source, there might be missing some skipping code (marked with 
XXX):
e.g. runNotifier.fireTestIgnored(description)

[org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl:3
43]
----------------------- 8< ----------------------
                if (throwableName.equals("org.junit.internal.AssumptionViolatedException") || throwableName.startsWith("org.junit.Assume$AssumptionViolatedException")) {
                    XXXXXXXXXXXX
                    return;
                } else if (!(Boolean) Whitebox.invokeMethod(testMethod, "expectsException")) {
----------------------- 8< ----------------------

Original comment by openid4...@gmail.com on 29 Jul 2013 at 1:29

GoogleCodeExporter commented 9 years ago
Please provide a patch  and I'll include it in the next release.

Original comment by johan.ha...@gmail.com on 30 Jul 2013 at 5:04