服务器上怎做网站,网上快速学做网站,网站推广策划方案3000字,怎么做php网站如Nifty JUnit#xff1a;使用临时文件一文中所示 #xff0c;可以在JUnit测试中使用Rule #xff0c;这是方法级别的规则。 在此示例中#xff0c;我想显示ClassRule用于类级别规则的变体。 方法规则 Rule在测试类的每个测试方法#xff08;就像Before #xff09;之前… 如Nifty JUnit使用临时文件一文中所示 可以在JUnit测试中使用Rule 这是方法级别的规则。 在此示例中我想显示ClassRule用于类级别规则的变体。 方法规则 Rule在测试类的每个测试方法就像Before 之前以及在测试类的每个测试方法就像After之后之后被激发如下例所示。 JUnitRuleTest package com.jdriven;import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;import java.io.File;
import java.io.IOException;public class JUnitRuleTest {//The Folder will be created before each test method and (recursively) deleted after each test method.Rulepublic TemporaryFolder temporaryFolder new TemporaryFolder();Testpublic void testJUnitRule() throws IOException {File tempFile temporaryFolder.newFile(tempFile.txt);//Your test should go here.}
}班级规则 除了常规的Rule之外我们还可以创建一个ClassRule 。 在TemporaryFolder的示例中这将导致在所有测试方法就像BeforeClass 之前创建一个文件夹并在所有测试方法就像AfterClass一样之后销毁该文件夹。 在下面的示例中您可以创建一个临时文件并在所有测试方法中使用完全相同的文件。 完成所有测试方法后该临时文件将被删除。 JUnitClassRuleTest package com.jdriven;import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;import java.io.File;
import java.io.IOException;public class JUnitClassRuleTest {//The Folder will be (recursively) deleted after all test.ClassRulepublic static TemporaryFolder temporaryFolder new TemporaryFolder();public static File tempFile;BeforeClasspublic static void createTempFile() throws IOException {tempFile temporaryFolder.newFile(tempFile.txt); //The tempFile will be deleted when the temporaryFolder is deleted.}Testpublic void testJUnitClassRule_One() {//Your test should go here, which uses tempFile}Testpublic void testJUnitClassRule_Two() {//Your test should go here and uses the same tempFile}
}翻译自: https://www.javacodegeeks.com/2015/03/nifty-junit-using-rule-on-method-and-class-level.html