JackYin 2020-01-24
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency>
import org.junit.Assert; /** * @author wzm * @version 1.0.0 * @date 2020/1/24 15:25 **/ public class MyTest { private static void test(int x) { int c = 10 + 20; int a = 12 * 100 / x; Assert.assertTrue("expected a == c ,but a != c", a == c); System.out.println("a == c"); } public static void main(String[] args) { int x = 10; test(x); } } 报错如下: 期望的结果: import org.junit.Assert; /** * @author wzm * @version 1.0.0 * @date 2020/1/24 15:25 **/ public class MyTest { private static void test(int x) { int c = 10 + 20; int a = 12 * 100 / x; Assert.assertTrue("expected a == c ,but a != c", a == c); System.out.println("a == c"); } public static void main(String[] args) { int x = 40; test(x); } } 运行结果: 断言还有很多其他方法。 assertTrue assertFalse assertEquals assertNotEquals assertArrayEquals ...
import org.junit.*; public class MyTestTest { public MyTestTest() { System.out.println("--new MyTestTest()"); } @BeforeClass public static void setUpBeforeClass() { System.out.println("--BeforeClass()"); } @AfterClass public static void tearDownAfterClass() { System.out.println("--AfterClass()"); } @Before public void setUp() { System.out.println("--Before()"); } @After public void tearDown() { System.out.println("--After()"); } @Test public void testA() { System.out.println("--testA()"); } @Test public void testB() { System.out.println("--testB()"); } @Test public void testC() { System.out.println("--testC()"); } }
@Test(timeout = 1000) public void testA() { System.out.println("--testA()"); }
@Test(expected = ArithmeticException.class) public void testException() { int i = 1 / 0; }
@Test(expected = ArithmeticException.class) public void testException() { int i = 1 / 1; }
java.lang.AssertionError: Expected exception: java.lang.ArithmeticException