среда, 1 августа 2012 г.

testing: junit - arraylisttest


import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import java.util.ArrayList;
import java.util.NoSuchElementException;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;

@RunWith(BlockJUnit4ClassRunner.class)
public class ArrayListEmptyTest {
    private ArrayList<Integer> emptyList;

    @Before
    public void setUp() {
        emptyList = new ArrayList<Integer>();
    }

    @Test
    public void testSizeIsZero() {
        assertThat(emptyList.size(), is(0));
    }

    @Test
    public void testIsEmpty() {
        assertThat(emptyList.isEmpty(), is(true));
    }

    @Test
    public void testClean() {
        emptyList.clear();
        assertThat(emptyList.isEmpty(), is(true));
        assertThat(emptyList.size(), is(0));
    }

    @Test
    public void testAddOne() {
        emptyList.add(123456);
        assertThat(emptyList.isEmpty(), is(false));
        assertThat(emptyList.size(), is(1));
        assertThat(emptyList.get(0), is(123456));
    }

    @Test
    public void testIteratorNotNull() {
        assertThat(emptyList.iterator(), is(not(null)));
    }

    @Test
    public void testIteratorHasElementFalse() {
        assertThat(emptyList.iterator().hasNext(), is(false));
    }

    @Test(expected = NoSuchElementException.class)
    public void testIteratorNextThrowException() {
        emptyList.iterator().next();
    }
}