How to Mark Cucumber Junit Step as Failed But Continue

How to make ignored junit steps in cucumber-jvm cause test scenario to fail

I presume you have a JUnit test case with an @CucumberOptions annotation on it. If you have this, you should be able to make ignored tests fail the build by setting strict=true. e.g.

@RunWith(Cucumber.class)
@CucumberOptions(strict = true)
public class CucumberRunnerTest {
}

How to ignore failure/skipping statement in cucumber for next than statements

You can either handle it using try - - - catch block or you can use soft assertion

Soft Assertions are the type of assertions that do not throw an exception when an assertion fails and would continue with the next step after assert statement.
This is usually used when our test requires multiple assertions to be executed and the user want all of the assertions/codes to be executed before failing/skipping the tests.
AssertJ is library providing fluent assertions. It is very similar to Hamcrest which comes by default with JUnit. Along with all the asserts AssertJ provides soft assertions with its SoftAssertions class inside org.assertj.core.api package

Consider the below example:

public class Sample {
@Test
public void test1() {
SoftAssert sa = new SoftAssert();
sa.assertTrue(2 < 1);
System.out.println(“Assertion Failed”);
sa.assertFalse(1 < 2);
System.out.println(“Assertion Failed”);
sa.assertEquals(“Sample”, “Failed”);
System.out.println(“Assertion Failed”);
}
}

Output:

Assertion Failed
Assertion Failed
Assertion Failed

PASSED: test1

Even now the test PASSED instead of FAILED. The problem here is the test would not fail when an exception is not thrown. In order to achieve the desired result we need to call the assertAll() method at the end of the test which will collate all the exceptions thrown and fail the test if necessary.



Related Topics



Leave a reply



Submit