01 bjsxt junit introduction
alt+/ 给出提示
被测试类
package com.bjsxt.junit4;
public class T {
public int add(int x, int y) {
return x + y;
}
public int divide(int x, int y) {
return x/y;
}
public static void main(String[] args) {
int z = new T().add(3, 5);
System.out.println(z);
}
}
junit类
package com.bjsxt.junit4.test;
import static org.junit.Assert.*;/JDK1.5版本后,可使用静态引入,直接使用类中的方法,不需要类名了
import static org.hamcrest.Matchers.*;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.AfterClass;
import org.junit.Ignore;
import org.junit.Test;
import com.bjsxt.junit4.T;
public class TTest {
@BeforeClass
public static void beforeClass() {
System.out.println("beforeClass");
}
@AfterClass
public static void afterClass() {
System.out.println("afterClass");
}
@Before
public void before() {
System.out.println("before");
}
@Test
public void testAdd() {
int z = new T().add(5, 3);
assertThat(z, is(8));/静态引入,直接使用类中的方法,不需要类名了
assertThat(z ,allOf(greaterThan(5), lessThan(10)));
//int a = 8/0;
}
@Test(expected=java.lang.ArithmeticException.class, timeout=100)
public void testDivide() {
int z = new T().divide(8, 0);
}
@After
public void after() {
System.out.println("after");
}
}
浙公网安备 33010602011771号