我正在準備一個測試用例場景,我在properties
文件中將這些值與他們的密鑰進行了測試。我希望如果所有關鍵值都滿足其相應的值,該方法應該返回true,否則返回false。如何使用junit中的屬性文件測試參數
下面是我的代碼:
package mypackTest;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import static org.junit.Assert.assertThat;
@RunWith(Parameterized.class)
public class Test123 {
static Properties p = null;
private boolean expected;
@Parameters
public static List<String[]> data()
{
String[][] data = new String[][] { { "D:\\personal\\config.properties" }};
return Arrays.asList(data);
}
@BeforeClass
public static void setup()
{
p = new Properties();
try
{
//load a properties file
p.load(new FileReader(new File("D:\\personal\\config.properties")));
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
@Test
public void do_test() {
assertThat(TestThis.letstest(p.getProperty("abc1"), p.getProperty("abc2")), is(expected));
}
}
,我想在測試類:
package mypackTest;
public class TestThis {
public static boolean letstest(String abc1,String abc2){
if(abc1.equals("xyz1")&& abc2.equals("xyz2")){
return true;
}
return false;
}
}
屬性文件
abc1=xyz1
abc2=xyz2
的TestRunner類
package mypackTest;
import java.util.Enumeration;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import junit.framework.TestFailure;
import junit.framework.TestResult;
import junit.framework.TestSuite;
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(CalculatorTest.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
我得到以下輸出
輸出
do_test0:錯誤的參數數目
假