[GRADLE-1879] org.junit.Assume isn't handled correctly sometimes Created: 30/Oct/11  Updated: 04/Jan/13  Resolved: 30/Oct/11

Status: Resolved
Project: Gradle
Affects Version/s: 1.0-milestone-3
Fix Version/s: None

Type: Bug
Reporter: Andrew Spina Assignee: Unassigned
Resolution: Won't Fix Votes: 0


 Description   

When I run gradle build on a file like this:

import org.junit.Assume;

public class MyAssumptionJUnitTest extends TestCase {

public void testAssumeClause()

{ Assume.assumeTrue( false ); }

}

The build fails unexpectedly. There is a stacktrace as follows in the test report:

org.junit.Assume$AssumptionViolatedException: got: <false>, expected: is <true>
at org.junit.Assume.assumeThat(Assume.java:42)
at org.junit.Assume.assumeTrue(Assume.java:54)
at edu.stsci.util.Assumptions.assumeThatNetworkConnectionAvailable(Assumptions.java:17)
at edu.stsci.hst.apt.controller.HstServerAvailabilityJUnitTest.testBot2MassAvailable(HstServerAvailabilityJUnitTest.java:64)
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 junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:81)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:51)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:63)

=======

Joern Huxhorn replied to my query on the user list with the following:

The problem below is not caused by Gradle but by a misuse of JUnit.

The following code does not fail with Gradle 1.0-m5 and JUnit 4.10:

import org.junit.Test;

import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;

public class AssumptionTest {
@Test
public void foo()

{ assumeTrue(false); fail("Will not fail."); }

}

The problem is caused by "extends TestCase", i.e. using JUnit 3.8 functionality.
Assume was added in JUnit 4 so the JUnit38ClassRunner does not know that AssumptionViolatedException does not imply a fail but should result in an ignored test instead.

Beside that, one could probably argue that tests ignored due to unmet assumptions should print something during Gradle build. I like the extreme noiselessness of Gradle, though.

Cheers,
Joern.



 Comments   
Comment by Joern Huxhorn [ 30/Oct/11 ]

The problem is not caused by Gradle but by a misuse of JUnit.

The following code does not fail with Gradle 1.0-m5 and JUnit 4.10:

 
import org.junit.Test;

import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;

public class AssumptionTest {
   @Test
   public void foo() {
       assumeTrue(false);
       fail("Will not fail.")
   }
}

The problem is caused by "extends TestCase", i.e. using JUnit 3.8 functionality.
Assume was added in JUnit 4 so the JUnit38ClassRunner does not know that AssumptionViolatedException does not imply a fail but should result in an ignored test instead.

Beside that, one could probably argue that tests ignored due to unmet assumptions should print something during Gradle build. I like the extreme noiselessness of Gradle, though.

Comment by Joern Huxhorn [ 30/Oct/11 ]

Whoops, you mentioned that already.

Comment by Peter Niederwieser [ 30/Oct/11 ]

Joern is right. Don't mix JUnit 3 (TestCase) with JUnit 4 (Assume).

Comment by Joern Huxhorn [ 31/Oct/11 ]

This version of the code

import junit.framework.TestCase;

import static org.junit.Assume.assumeTrue;

public class AssumptionTest extends TestCase {

    public void testFoo() {
        assumeTrue(false);
        fail("Will not fail.");
    }
}

fails in IDEA in the same way, i.e.

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 AssumptionTest.testFoo(AssumptionTest.java:11)
Comment by Andrew Spina [ 04/Nov/11 ]

I ran a similar test in Eclipse and found that JUnit 3 tests don't function correctly with Assume. I agree that this issue should not be fixed.

Generated at Wed Jun 30 12:06:49 CDT 2021 using Jira 8.4.2#804003-sha1:d21414fc212e3af190e92c2d2ac41299b89402cf.