简体   繁体   中英

assumeTrue in JUnit4 is erring instead of ignoring tests

I'm using JUnit 4 (junit-4.8.2.jar) to run tests on my Java project and am running into problems when using assumeTrue. I have this basic set up:

public class ClientTest extends TestCase {

    @BeforeClass
    public void setUp() {
        assumeTrue(isPaidAccount());
    }

    @Test
    public void testTheThings() {
        // run this test if the user has a paid account
    }
}

The method isPaidAccount() is coming back false, which is expected, but instead of the tests in the class being ignored, they're all coming back as errors. This is happening in Eclipse with the JUnit4 Test Runner as well as with maven. I have also tried moving the assumeTrue into a @Before method like so:

public class ClientTest extends TestCase {

    @BeforeClass
    public void setUp() {
        // set up
    }

    @Before
    public void mustHavePaidAccount() {
        assumeTrue(isPaidAccount());
    }

    @Test
    public void testTheThings() {
        // run this test if the user has a paid account
    }
}

However, when I do this it completely ignores the @Before method and just runs the tests anyway. Again, I'm getting the same behavior with both the Eclipse Test Runner and the maven one. I'm sure I'm doing something obviously incorrect, but I'm not sure what it is.

Extending TestCase is for JUnit 3.x and using annotations is JUnit 4.x. If you drop the extends TestCase it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM