Java – how to ignore unit tests when conditions are met?

I wonder if there are comments or methods that can only execute tests if pre conditionoin is satisfied?

Solution

You can use assume to do this

In the example shown below, I want to check the status if precondition = = true and I want to assert that an exception is thrown when precondition = = false

@Test
public final void testExecute() throws InvalidSyntaxException {
    Assume.assumeTrue(precondition);  // Further execution will be skipped if this holds true
    CommandResult result = sentence.getCommand().execute();
    boolean status = Boolean.parseBoolean(result.getResult());
    Assert.assertTrue(status);
}

@Test(expected = InvalidSyntaxException.class)
public final void testInvalidParse() throws InvalidSyntaxException {
    Assume.assumeTrue(!precondition);
    CommandResult result = sentence.getCommand().execute();
}

I hope it will help you

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>