javaunittest怎么写

1.怎样创建一个java Unit test往简单了说,在没有返回值的方法前加上@Test,然后将junit的jar包引入就好了 。。这个ide都会有的,直接引入就行了,简单代码如下:然后run as junit test就可以运行了
public class IntegerTest {
@Test
public void test() {
Integer i1 = new Integer(2) ;
Integer i2 = new Integer(2) ;
Integer i3 = 35;
Integer i4 = 35;
System.out.println(i3==i4);
}
}
2.java junit单元测试怎么写package com.yuanqi.zfb.test;
import org.junit.Test;
import com.yuanqi.zfb.util.VerifyCodeUtils;
public class Atest {
@Test
public void test(){
String verifycode =VerifyCodeUtils.generateVerifyCode(8);
System.out.println(verifycode);
}
@Test
public void test2(){
// String str="2015-11-23 11:23:44";
/* boolean b= str.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}");
if(b){
System.out.println("ok");
}else{
System.out.println("222222");
}*/
String str="2015112311:23:44";
boolean b= str.matches("\\d{10}:\\d{2}:\\d{2}");
System.out.println(b);
}
@Test
public void test3(){
String trTime="2014112800:05:48";
String inyear=trTime.substring(0, 4);
String inmonth=trTime.substring(4,6);
String inday=trTime.substring(6,8);
String intime=trTime.substring(8);
String time=inyear+"-"+inmonth+"-"+inday+" "+intime;
System.out.println(time);
}
}
3.Java Junit Test 要怎么写一般不用写,直接测试方法,在测试的方法上加上注解@Test
import org.junit.Test;
public class TestJunit {
@Test
public void TestSaveMethod(){
Food food=new Food("红烧肉",new BigDecimal(45.5).setScale(2, BigDecimal.ROUND_HALF_UP), "hsr.jpg");
SaveFood(food);
}
public void SaveFood(Food food){
if(food!=null){
food.setId(UUID.randomUUID().toString());
System.out.println("Food Save is OK!");
System.out.println("当前ID:"+food.getId()+" 名称:"+food.getName()+" 单价:"+food.getPrice());
}else{
System.out.println("Food Save is False!");
}
}
}
class Food implements Serializable{
private String id;
private String name;
private BigDecimal price;
private String icon;
public Food(String name,BigDecimal price,String icon) {
this.name=name;
this.price=price;
this.icon=icon;
}
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public BigDecimal getPrice() {
return price;
}
public String getIcon() {
return icon;
}
}
//这样的话RUN 的时候就会出现JUnit测试
4.java 怎么写junit测试用例文件名:Calutor.java
package com.sc.zy;
public class Calutor {
public int add(int num1,int num2){
return num1+num2;
}
public int sub(int num1,int num2){
return num1-num2;
}
public int mul(int num1,int num2){
return num1*num2;
}
public int div(int num1,int num2){
if(num2==0){
throw new MyException();
}
return num1/num2;
}
}
文件名:MyException.Java
package com.sc.zy;
public class MyException extends RuntimeException {
}
文件名:CalutorTest.java
package com.sc.zy;
import junit.framework.Assert;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
public class CalutorTest {
private Calutor c;
@BeforeClass
【javaunittest怎么写】public static void setUpBeforeClass(){