简体   繁体   English

假设JUnit4中的True错误而不是忽略测试

[英]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. 我正在使用JUnit 4(junit-4.8.2.jar)在Java项目上运行测试,并且在使用假定True时遇到问题。 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. 方法isPaidAccount()返回false,这是预期的,但是不是忽略类中的测试,而是将它们作为错误返回。 This is happening in Eclipse with the JUnit4 Test Runner as well as with maven. 这是在Eclipse中使用JUnit4 Test Runner以及maven进行的。 I have also tried moving the assumeTrue into a @Before method like so: 我也尝试过将consumpTrue移到@Before方法中,如下所示:

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. 但是,当我这样做时,它会完全忽略@Before方法,并且无论如何都将运行测试。 Again, I'm getting the same behavior with both the Eclipse Test Runner and the maven one. 同样,Eclipse Test Runner和Maven的行为相同。 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. 扩展TestCase适用于JUnit 3.x,使用批注的是JUnit4.x。 If you drop the extends TestCase it should work. 如果删除extends TestCase它将正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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