我有一个java类,它包含一些关于整数数组中二分搜索的方法。 现在我应该创建一个新类来验证这些方法的正确性,用不同的数组和不同的元素来测试这些方法。 现在,我以“标准”方式完成了这个新的测试类,但我知道我可以使用JUnit。 我在网上搜索了一些简单的指南,但我并没有太多了解。关于JUnit的疑问和建议
例如,如果我的班 “主力” BinarySearch.java这样制成:
public class BinarySearch {
private BinarySearch() { }
public static int find(int x, int[] a) {
int i = 0;
int inf = 0;
int sup = a.length - 1;
if(sup == -1 || x < a[0] || x > a[sup])
return -1;
while(inf <= sup) {
i = (inf + sup) >>> 1;
if(x < a[i])
sup = i - 1;
else if(x > a[i])
inf = i + 1;
else
return i;
}
return -1;
}
public static boolean isPresent(int x, int[] a) {
int i = 0;
int inf = 0;
int sup = a.length - 1;
if(sup == -1 || x < a[0] || x > a[sup])
return false;
while(inf <= sup) {
i = (inf + sup) >>> 1;
if(x < a[i])
sup = i - 1;
else if (x > a[i])
inf = i + 1;
else
return true;
}
return false;
}
public static void sort(int[] a) {
int b, c;
int temp;
int s = a.length - 1;
for(b = 0; b < s; ++b) {
for(c = 0; c < s; ++c) {
if(a[c] < a[c + 1]) {
temp = a[c];
a[c] = a[c + 1];
a[c + 1] = temp;
}
}
}
}
public static boolean isSorted(int[] a) {
for(int i = 0; i < a.length-1; i++) {
if(a[i] > a[i + 1]) {
return false;
}
}
return true;
}
public static void isort(int a[]) {
for(int i = 1; i < a.length; i++){
int j = i;
int b = a[i];
while(j > 0 && a[j - 1] > b) {
a[j] = a[j - 1];
j--;
}
a[j] = b;
}
}
}
测试类 “标准” 是:
public class BinarySearchTestSuite {
private BinarySearchTestSuite() {}
static int tot = 0;
static int pass = 0;
static int nonPass = 0;
static int ecc = 0;
public static void main(String[] args) {
int[] a1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] a2 = {};
int x1 = 5;
int x2 = 0;
int x3 = 10;
System.out.println();
System.out.println("---- Test method find ----");
//Test n.1
try {
tot++;
System.out.print("Test n.1 - Array: [ ");
for(int i = 0; i < a1.length; i++)
System.out.print(a1[i] + " ");
System.out.println("]. Element to find: " + x1 + ".");
if(a1[BinarySearch.find(x1, a1)] == x1) {
System.out.println(x1 + " is in position " + BinarySearch.find(x1, a1) + ".");
System.out.println("Test OK.");
pass++;
}
else if(BinarySearch.find(x1, a1) == -1) {
System.out.println(x1 + " is not present in array");
System.out.println("TTest OK.");
pass++;
}
else {
System.out.println("Test FAIL.");
nonPass++;
}
} catch(Exception eccezione) {
System.out.println("Test FAIL.");
ecc++;
}
System.out.println();
//Test n.2
try {
tot++;
System.out.print("Test n.2 - Array: [ ");
for(int i = 0; i < a1.length; i++)
System.out.print(a1[i] + " ");
System.out.println("]. Element to find: " + x2 + ".");
if(a1[BinarySearch.find(x2, a1)] == x2) {
System.out.println(x2 + " is in position " + BinarySearch.find(x2, a1) + ".");
System.out.println("Test OK.");
pass++;
}
else if(BinarySearch.find(x1, a1) == -1) {
System.out.println(x1 + " is not present in array");
System.out.println("Test OK.");
pass++;
}
else {
System.out.println("Test FAIL.");
nonPass++;
}
} catch(Exception eccezione) {
System.out.println("Test FAIL.");
ecc++;
}
System.out.println();
//RESULT
System.out.println();
System.out.println("Test totali: " + tot);
System.out.println("Test andati a buon fine: " + pass);
System.out.println("Test falliti: " + nonPass);
System.out.println("Test falliti con lancio di una eccezione: " + ecc);
}
}
如何创建一个测试类与JUnit? 我需要一些简单的例子。
我知道,在测试类我必须写这样的代码:
private final ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
private final ByteArrayOutputStream errBuffer = new ByteArrayOutputStream();
@Before
public void setupOutputStreams() {
System.setOut(new PrintStream(outBuffer));
System.setErr(new PrintStream(errBuffer));
}
@After
public void cleanupOutputStreams() {
System.setOut(null);
System.setErr(null);
}
此代码后,我怎么写了单独测试的代码? 然后我通常编译并运行这个文件(BinarySearchTest.java)?或者有另一种方法可以做到这一点? 我有点困惑...
谢谢!
感谢
大概检查您用于Junit集成的IDE。基本上,你不会编写自己的程序来运行Junit测试,但是你可以用@Test扩展一个类并注释测试方法,让Junit为你做好准备。 – TeTeT
[如何编写单元测试?]的可能重复(http://stackoverflow.com/questions/8751553/how-to-write-a-unit-test) – Joe