'junit Test 순서'에 해당되는 글 1건

  1. 2010.08.20 Junit 오해

Junit 오해

general java 2010.08.20 11:51

Junit 에서 @Test 코드 실행시 코드상에서 순차적으로 실행되는 것으로 생각되는 분들이 많은데.
사실은.. 순서를 보장하지 않는다.
참고 내용
http://junit.sourceforge.net/doc/faq/faq.htm

    package junitfaq;

    import org.junit.*;
    import static org.junit.Assert.*;
    import java.util.*;

    public class SimpleTest {

        private Collection<Object> collection;

        @Before
        public void setUp() {
            collection = new ArrayList<Object>();
        }

        @Test
        public void testEmptyCollection() {
            assertTrue(collection.isEmpty());
        }


        @Test
        public void testOneItemCollection() {
            collection.add("itemA");
            assertEquals(1, collection.size());
        }
    }
      

Given this test, the methods might execute in the following order:

setUp()
testEmptyCollection()
setUp()
testOneItemCollection()

The ordering of test-method invocations is not guaranteed, so testOneItemCollection() might be executed before testEmptyCollection(). But it doesn't matter, because each method gets its own instance of the collection.

Posted by 김용환 '김용환'