junit 4.11 Hamcrest

2507 단어 JUnit
Hamcrest 다운로드 주소는 다음과 같습니다.https://code.google.com/p/hamcrest/downloads/list
Junit 테스트를 강화하는 방법을 많이 제공했는데 Junit 4.11에 이미 추가되었다

import java.util.HashMap;
import java.util.Map;

import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;



public class TestByHamcrest {
    /**
     *      JUnit-4.10,        org.junit.Assert ,    assertThat   
     *      JUnit   , MyEclipse6.5   JUnit-4.3.1    Assert.assertThat  
     *       hamcrest-all-1.3.jar   org.hamcrest.MatcherAssert.assertThat()  
     */
    @Test
    public void testHamcrest() {
        // 50    20
        Assert.assertThat(50, Matchers.greaterThan(20));
        // 50       50
        Assert.assertThat(50, Matchers.greaterThanOrEqualTo(50));

        // 50     20   60(allOf     Java  &&)
        Assert.assertThat(50, Matchers.allOf(Matchers.greaterThan(20), Matchers.lessThan(60)));
        // 50    20   40(anyOf     Java  ||)
        Assert.assertThat(50, Matchers.anyOf(Matchers.greaterThan(20), Matchers.lessThan(40)));

        //   "abc.txt"   "txt"  
        Assert.assertThat("abc.txt", Matchers.endsWith("txt"));
        Assert.assertThat("abc.txt", Matchers.startsWith("ab"));
        Assert.assertThat("abc.txt", Matchers.containsString("c.t"));
        Assert.assertThat(22 + "aa", Matchers.hasToString("22aa"));
        Assert.assertThat("http://blog.csdn.net/jadyer", Matchers.instanceOf(String.class));
        Assert.assertThat("http://blog.csdn.net/jadyer", Matchers.notNullValue());
        Assert.assertThat(null, Matchers.nullValue());

        //        
        String[] users = {"  ", "Jadyer"};
        Assert.assertThat(users, Matchers.hasItemInArray("  "));
        Map<String, String> userMap = new HashMap<String, String>();
        userMap.put("11", "  ");
        userMap.put("22", "Jadyer");
        userMap.put("33", "http://blog.csdn.net/jadyer");
        Assert.assertThat(userMap, Matchers.hasKey("22"));
        Assert.assertThat(userMap, Matchers.hasValue("http://blog.csdn.net/jadyer"));
    }


좋은 웹페이지 즐겨찾기