1.Junit下载
下载junit的jar包,网址:https://junit.org/
(可以先忽略下载,因为有的eclipse会自带jar包,如果下面的某一步操作有错误没有该jar包,再进行下载)
2.配置
选中要添加测试的工程
例如下图的shapes
然后Project -> Properties
依次点击Java Build Path,Libraries,Add Library
然后选择Junit,next
自行选择Junit版本
点击finish,Apply and Close即可成功添加
3.新建单元测试
在要添加测试的项目上右键点击new -> other
找到Junit下的Junit Test Case
点击next
然后填写好Name,finish即可。可以看到,多了一个.Java文件
可以在里面编写测试代码进行测试。
4.编写测试用例
以上一篇博客代码为例,为其编写测试代码。
注:
1)一般起名为函数名+Test
2)具体测试前面要加@Test
3)assertEquals( , )和assertTrue()是常用的测试函数
4.1 addVertexTest()
测试思路:新建person类(ABCD),然后调用addVertex()将点添加进去,然后测试加进去的点是否存在。
@Test
public void addVertexTest() {//add顶点测试
FriendshipGraph graph = new FriendshipGraph();
Person A = new Person("A");
Person B = new Person("B");
Person C = new Person("C");
Person D = new Person("D");
graph.addVertex(A);
graph.addVertex(B);
graph.addVertex(C);
graph.addVertex(D);
assertTrue(graph.namelist.contains(A));
assertTrue(graph.namelist.contains(B));
assertTrue(graph.namelist.contains(C));
assertTrue(graph.namelist.contains(D));
}
4.2 addEdgeTest()
测试思路:将测试图的边填写到对应邻接矩阵,即有边赋为1。然后新建person点,然后调用 addEdge()将边对应的两点添加进去,判断生成的邻接矩阵与正确的邻接矩阵是否一样。
@Test
public void addEdgeTest() //add边测试
{
FriendshipGraph graph = new FriendshipGraph();
int edge1[][] = new int[10000][10000];//应该得到的正确邻接矩阵
edge1[0][1] = 1;
edge1[1][0] = 1;
edge1[1][2] = 1;
edge1[2][1] = 1;
edge1[1][3] = 1;
edge1[3][1] = 1;
edge1[0][2] = 1;
edge1[2][0] = 1;
Person A = new Person("A");
Person B = new Person("B");
Person C = new Person("C");
Person D = new Person("D");
graph.addVertex(A);
graph.addVertex(B);
graph.addVertex(C);
graph.addVertex(D);
graph.addEdge(A, B);
graph.addEdge(B, A);
graph.addEdge(B, C);
graph.addEdge(C, B);
graph.addEdge(D, B);
graph.addEdge(B, D);
graph.addEdge(A, C);
graph.addEdge(C, A);
assertEquals(graph.edge, edge1);
4.3 getDistanceTest()
测试思路:首先类似于4.1,4.2新建一个有向图,然后利用getDistance()函数计算出两点间的距离,然后与正确的距离比较是否一样。
有向图如下:
@Test
public void getDistanceTest() //比main中更复杂的图的测试
{
FriendshipGraph graph = new FriendshipGraph();
for(int i=0; i<graph.edge.length; i++)//初始化graph对象的邻接矩阵
{
for(int j=0; j<graph.edge.length; j++)
{
graph.edge[i][j] = 1000;
}
}
Person A = new Person("A");
Person B = new Person("B");
Person C = new Person("C");
Person D = new Person("D");
Person E = new Person("E");
Person F = new Person("F");
Person G = new Person("G");
Person H = new Person("H");
graph.addVertex(A);
graph.addVertex(B);
graph.addVertex(C);
graph.addVertex(D);
graph.addVertex(E);
graph.addVertex(F);
graph.addVertex(G);
graph.addVertex(H);
graph.addEdge(A, B);
graph.addEdge(B, A);
graph.addEdge(B, D);
graph.addEdge(D, B);
graph.addEdge(B, E);
graph.addEdge(D, E);
graph.addEdge(E, D);
graph.addEdge(E, C);
graph.addEdge(C, E);
graph.addEdge(C, F);
graph.addEdge(F, C);
graph.addEdge(E, F);
graph.addEdge(F, E);
graph.addEdge(G, H);
graph.addEdge(H, G);
assertEquals(graph.getDistance(A, C), 3);
assertEquals(graph.getDistance(A, F), 3);
assertEquals(graph.getDistance(E, E), 0);
assertEquals(graph.getDistance(D, F), 2);
assertEquals(graph.getDistance(C, E), 1);
assertEquals(graph.getDistance(B, F), 2);
assertEquals(graph.getDistance(A, H), -1);
assertEquals(graph.getDistance(D, G), -1);
}