以算数中加法乘法说明Junit怎样使用:
package com.gxc.cal;
public class Calu {
public int add(int n1,int n2){
return n1+n2;
}
public int plus(int n1,int n2){
return n1*n2;
}
}
Idea中按“ctrl + shift + t”开启单元测试:
在单元测试中加入Assert断言方法
package com.gxc.cal;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public class CaluTest {
Calu c = new Calu();
@Test
public void add() {
int res = c.add(2, 3);
Assert.assertEquals("OK", 5, res);
}
@Test
public void plus() {
Assert.assertEquals("OK", 6, c.plus(2, 3));
}
}