JavaSwing技术

junit搭配hamcrest使用

2017-05-18  本文已影响0人  LinkedIn

开篇

依赖:junit 4.4
hamcrest 1.3 core hamcrest 1.3 lib

Paste_Image.png Paste_Image.png Paste_Image.png
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.hamcrest.Matchers.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class DateTest2 {
    private Date allDate;
    private ByteArrayOutputStream bytes;
    @Before
    public void setUp() throws Exception {
        allDate=new Date(5, 18, 2017);
        bytes = new ByteArrayOutputStream();
        System.setOut(new PrintStream(bytes));
    }

    @After
    public void tearDown() throws Exception {
        System.setOut(System.out);
    }


    @Test
    public void testDate() {
        Date date=new Date(5, 18, 2017);
        assertEquals(true, date instanceof Date);
    }

    @Test
    public void testIncrement() {
        allDate.increment();
        System.out.println();
        assertEquals(6, allDate.getMonth().getMonth());
    }
    @Test
    public void testPrintDate() {
        allDate.printDate();
        assertThat(bytes.toString(),is("5/18/2017\r\n"));
    }


    @Test
    public void testGetDay() {
        fail("Not yet implemented");
    }

    @Test
    public void testGetMonth() {
        fail("Not yet implemented");
    }

    @Test
    public void testGetYear() {
        fail("Not yet implemented");
    }

    @Test
    public void testEqualsObject() {
        fail("Not yet implemented");
    }

    @Test
    public void testToString() {
        fail("Not yet implemented");
    }

}

assertEquals(String msg, Object expectRes, Object Res) --------  用于值判断

判断expectRes.equals(Res) ,表示值等于的判断,失败则抛MSG

assertSame(String msg, Object expectRes, Object Res)  --------  用于地址判断

判断expectRes==Res,表示地址等于的判断,失败则抛MSG

assertTrue(String msg,Boolean result) ----------------------------用于Boolean判断

判断result是true还是false,失败则抛MSG

assertNull(String msg,Object result)-------------------------------用于NULL判断

判断result是否为NULL,失败则抛MSG

fail(String msg);---------------------------------------------------直接中止方法运行

直接中止方法运行,抛出MSG

显示绿条,意味测试通过


Paste_Image.png
上一篇下一篇

猜你喜欢

热点阅读