본문 바로가기
개발/일반

junit list test

by 로그인시러 2016. 7. 29.
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.hamcrest.collection.IsEmptyCollection;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.hamcrest.MatcherAssert.assertThat;

public class ListTest {

    @Test
    public void testAssertList() {

        List actual = Arrays.asList("a", "b", "c");
        List expected = Arrays.asList("a", "b", "c");
		
		//All passed / true
		
        //1. Test equal.
        assertThat(actual, is(expected));

        //2. If List has this value?
        assertThat(actual, hasItems("b"));

        //3. Check List Size
        assertThat(actual, hasSize(3));

        assertThat(actual.size(), is(3));

        //4.  List order

        // Ensure Correct order
        assertThat(actual, contains("a", "b", "c"));

        // Can be any order
        assertThat(actual, containsInAnyOrder("c", "b", "a"));

        //5. check empty list
        assertThat(actual, not(IsEmptyCollection.empty()));

        assertThat(new ArrayList<>(), IsEmptyCollection.empty());

    }

}

출처 : https://www.mkyong.com/unittest/junit-how-to-test-a-list/

'개발 > 일반' 카테고리의 다른 글

UML 표기법  (0) 2016.11.24
JAVA arrays contains  (0) 2016.11.11
울트라 에디트 정규식  (0) 2016.11.09
[MAVEN] how to add external library  (0) 2016.09.29
[JAVA] array to list, list to array  (0) 2016.08.10

댓글