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
ファイル
テストランナークラス
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());
}
}
私は、出力の下に取得しています
OUTPUT
do_test0:引数の数が間違っ
偽