问题
I'm trying to write conditional tests with JUnit. I need to test the persistence of objects and if those tests pass, test access via HTTP methods (in order to develop a REST Web Service).
For this moment my solution looks like this :
public class ApplicationTest {
@Test
public void testSuite() {
final Request requestStatutTest = Request.method(this.getClass(), "insertStatutTest");
final Result resStatutTest = new JUnitCore().run(requestStatutTest);
if (resStatutTest.wasSuccessful()) {
postStatutTest();
getStatutTest();
putStatutTest();
deleteStatutTest();
}
public void insertStatutTest() {
}
public void postStatutTest() {
}
// etc...
}
Is it the good solution ?
回答1:
Rather than assert that the property is true, you can use org.junit.Assume
:
@Before
public void checkAssumptions() {
org.junit.Assume.assumeTrue(someCondition());
// or import static org.junit.Assume.* and then just call assumeTrue()
}
If the condition is false, then this will result in the test ending with a violated assumption. As the docs say:
A failed assumption does not mean the code is broken, but that the test provides no useful information.
This is a weaker condition than a failed assertion (i.e. a test failure), and the default JUnit runner will treat this test as ignored. Which sounds like exactly what you are looking for - run the test if the persistence works, otherwise ignore it.
来源:https://stackoverflow.com/questions/19358444/junit-how-to-make-conditional-tests