package org.shouke.demo;
publicclass BinarySearch {
publicint binarySearch(long[] a, long key) {
int low = 0;
int high = a.length - 1;
while (low <= high) {
int mid = (low + high) >>> 1;
long midVal = a[mid];
if (midVal < key)
low = mid + 1;
elseif (midVal > key)
high = mid - 1;
else
return mid;
}
return -1;
}
}
package org.shouke.test;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.shouke.demo.BinarySearch;
//@RunWith(SpringRunner.class)
@RunWith(JUnit4.class)
//@SpringBootTest
//@TestPropertySource("classpath:test-application.properties")
public class BinarySearchTest {
private BinarySearch binarySearch = new BinarySearch();
private long[] array1 = new long[] {};
@Test
public void testBinarySearch1() {
System.out.println("执行方法 testBinarySearch1");
int index = binarySearch.binarySearch(array1, 401L);
Assert.assertEquals(-1, index);
}
private long[] array2 = new long[] {123L,123L,123L,123L,123L,123L,123L,123L};
@Ignore
public void testBinarySearch2() {
System.out.println("执行方法 testBinarySearch2");
int index = binarySearch.binarySearch(array2, 401L);
Assert.assertEquals(-1, index);
}
private long[] array3 = new long[] {123L, 456L};
@Test
public void testBinarySearch3() {
System.out.println("执行方法 testBinarySearch3");
int index = binarySearch.binarySearch(array3, 401L);
Assert.assertEquals(-1, index);
}
private long[] array4 = new long[] {123L, 456L};
@Test
public void testBinarySearch4() {
System.out.println("执行方法 testBinarySearch4");
int index = binarySearch.binarySearch(array4, 40L);
Assert.assertEquals(-1, index);
}
private long[] array5 = new long[] {123L, 456L};
@Test
public void testBinarySearch5() {
System.out.println("执行方法 testBinarySearch5");
int index = binarySearch.binarySearch(array5, 123L);
Assert.assertEquals(0, index);
}
private long[] array6 = new long[] {123L, 123L};
@Test
public void testBinarySearch6() {
System.out.println("执行方法 testBinarySearch6");
int index = binarySearch.binarySearch(array6, 123L);
Assert.assertEquals(0, index);
}
@Before
public void testBeforeMethod() {
System.out.println("执行每个方法前先执行该函数");
}
@After
public void testAfterMethod() {
System.out.println("执行完每个方法后都会执行该函数");
}
@BeforeClass
public static void testBeforeClass() {
System.out.println("执行测试类的所有方法前先执行该函数");
}
@AfterClass
public static void testAfterClass() {
System.out.println("执行完测试类的所有方法后都会执行该函数");
}
}
package org.shouke.demo;
publicclass Caculator {
publicint caculate(int arg1, int arg2) {
if (arg1 > arg2) {
return arg1 - arg2;
} elseif (arg1 < arg2) {
return arg1 + arg2;
} else {
return arg1;
}
}
}
package org.keshou.test;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.shouke.demo.Caculator;
import java.util.Arrays;
import java.util.Collection;
@RunWith(Parameterized.class)
public class CaculatorTest {
private Caculator caculator = new Caculator();
public int arg1;
public int arg2;
public CaculatorTest(int arg1, int arg2) {
this.arg1 = arg1;
this.arg2 = arg2;
}
// @Parameterized.Parameters
@Parameterized.Parameters(name = "{index}: (arg1: {0} arg2: {1}")
public static Collectiondata() {
return Arrays.asList(new Object[][] {
{ 10, 1}, { 5, 1 }
});
}
@Test
publicvoid testCaculate1() {
int result = caculator.caculate(arg1, arg2);
System.out.println("执行方法 testCaculate1 参数:" + arg1 + " " + arg1);
Assert.assertEquals(result, arg1-arg2);
}
@Test
publicvoid testCaculate2() {
int result = caculator.caculate(arg1, arg2);
System.out.println("执行方法 testCaculate2 参数:" + arg1 + " " + arg1);
Assert.assertEquals(result, arg1+arg2);
}
}
说明:被@Parameters 注解修饰用于提供参数的方法有多少个参数,那么就需要为其所在类提供对应数量的类属性,及一个包含对应数量的参数的构造函数,否则会报错:java.lang.IllegalArgumentException: wrong number of arguments
package org.keshou.test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({CaculatorTest.class, org.shouke.test.BinarySearchTest.class})
public class RunAllTestClass {
}
说明:如果需要运行多个测试类,只需要把目标测试类名称.class放入如下的 {}中即可,测试类之间使用逗号分隔,如果不是同一个包中的测试类,记得加上对应的package名称,或者使用import提前导入对应类。
@Suite.SuiteClasses({A.class, B.class, ...})
如下图,右键整个项目、单个测试类、测试套件 -> Coverage As -> JUnit Test
或者
如下图,右键整个项目、单个测试类、测试套件 -> Run As -> JUnit Test
说明:
1、如果右键时选择的是整个项目,那么项目src\test\;font-size:10.5000pt;mso-font-kerning:0.0000pt;">目录下的都有测试类都会被执行。
2、Coverage as 和 Run as 这两种运行方式的区别在于前者运行完成,会在控制台端自动打开 Coverage 界面,展示覆盖率,后者需要手动打开,打开方式如下:
Window -> Show View -> Java -> Coverage
运行测试套件