一,概括
二,junit
三,案例
(1),实验类
package com.bilibili;
public class Name {
public static void main(String name) {
if (name == null){
System.out.println("0");
return;
}
System.out.println("名字长度为"+name.length());
}
public static int fangfa2 (String data){
if (data==null){
return -1;
}
return data.length()-2;
}
}
(2),测试类
package com.bilibili;
/*测试类
* */
import org.junit.Assert;
import org.junit.Test;
public class NameTest {
@Test// 测试方法
public void testmain (){
Name.main("admin");
Name.main(null);
}
@Test
public void testfangfa2() {
//普通测试
int index1=Name.fangfa2(null);
System.out.println(index1);
int index2=Name.fangfa2("dfhrtv");
System.out.println(index2);
// int index3=Name.fangfa2("ykjr tuybf 5678");
// System.out.println(index3);
//断言测试
Assert.assertEquals("方法有BUG",4,index2);
}
}
四,常见注解
五,案例
(1),测试类
public class NameTest {
@Before
public void test0(){
System.out.println("=====开始测试=====");
}
@BeforeClass
public static void test1(){
System.out.println("=====tu开始测试=====");
}
@After
public void test2(){
System.out.println("=====结束测试=====");
System.out.println(" ");
}
@AfterClass
public static void test3(){
System.out.println("=====ok结束测试=====");
System.out.println(" ");
}
@Test// 测试方法
public void testmain (){
Name.main("admin");
Name.main(null);
}
@Test
public void testfangfa2() {
//普通测试
int index1=Name.fangfa2(null);
System.out.println(index1);
int index2=Name.fangfa2("dfhrtv");
System.out.println(index2);
// int index3=Name.fangfa2("ykjr tuybf 5678");
// System.out.println(index3);
//断言测试
Assert.assertEquals("方法有BUG",4,index2);
}
}
(2),结果
=====tu开始测试=====
=====开始测试=====
名字长度为5
0
=====结束测试=====
=====开始测试=====
-1
4
=====结束测试=====
=====ok结束测试=====
六,常见注解注意:
junit5版本的注解名字与junit4不同