javajunit怎么写

1. 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
public static void setUpBeforeClass(){
System.out.println("=====static init=======");
}
@AfterClass
public static void tearDownAfterClass(){
System.out.println("=====static destory=======");
}
@Before
public void setUp(){
System.out.println("[email protected]=======");
c=new Calutor();
}
@After
public void tearDown(){
System.out.println("[email protected]=======");
}
@Test
public void testAdd(){
int sum=c.add(1, 2);
Assert.assertEquals(3, sum);
}
@Test(expected=com.sc.zy.MyException.class)
public void testDiv(){
c.div(1, 0);
}
@Ignore
public void testDiv1(){
int d=c.div(1, 5);
Assert.assertEquals(0, d);
}
}
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(){
【javajunit怎么写】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) {