Junit:如何进行条件测试?
我正在尝试使用JUnit编写条件测试.我需要测试对象的持久性,如果这些测试通过,请通过HTTP方法测试访问(以开发REST Web服务).
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 ?
您可以使用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.
与断言失败(即测试失败)相比,这是一个较弱的条件,并且默认的JUnit运行器会将此测试视为已忽略.听起来完全像是您要寻找的东西-如果持久性有效,请运行测试,否则将其忽略.
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.