Junit 基本使用

来源:互联网 发布:c语言入门自学软件 编辑:程序博客网 时间:2024/06/04 18:55
1. 安装Junit,导入下面的依赖包

<dependency>  <groupId>junit</groupId>  <artifactId>junit</artifactId>  <version>${junit.version}</version>  <scope>test</scope></dependency><dependency><groupId>org.hamcrest</groupId><artifactId>hamcrest-library</artifactId><version>1.3</version><scope>test</scope></dependency>


2.简单的测试类

package com.peter.user;import org.junit.After;import org.junit.Test;import static org.hamcrest.Matchers.*;import static org.junit.Assert.*;/** * Unit test for simple App. */public class AppTest {    @Test    public void AppTest( )    {    assertTrue("sdrt", 8>5);        assertThat(3, is(3));    }    @After    @Test(expected=java.lang.ArrayIndexOutOfBoundsException.class)    public void test2(){        }}


1.org.junit.Assert.assertThat(Integer actual, Matcher<? super Integer> matcher) 使用上面的断言方法可以替代其他断言方法
2.使用注解
@Test(expected=java.lang.ArrayIndexOutOfBoundsException.class) 测试是否抛出指定异常
@Before @After 在每个测试方法前后执行的方法,比如建立,断开数据库连接
@BeforeClass @AfterClass 在测试类执行前后执行
原创粉丝点击