Programing

setUp ()과 setUpBeforeClass ()의 차이점

lottogame 2020. 6. 8. 07:52
반응형

setUp ()과 setUpBeforeClass ()의 차이점


단위의 JUnit으로 테스트 할 때,이 두 개의 유사한 방법이 있습니다, setUp()하고 setUpBeforeClass(). 이 방법들 사이의 차이점은 무엇입니까? 또한, 차이 무엇 tearDown()tearDownAfterClass()?

서명은 다음과 같습니다.

@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
}

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@BeforeClass@AfterClass아무것도가 실행되기 전에, 전체 시험의 시작과 끝에서 - 주석이 방법은 테스트를 실행하는 동안 정확히 한 번만 실행됩니다. 실제로 테스트 클래스가 구성되기 전에 실행되므로 선언해야합니다 static.

@Before@After방법은 모든 테스트 케이스 전후에 실행됩니다, 그래서 아마 테스트를 실행하는 동안 여러 번 실행됩니다.

따라서 클래스에 세 가지 테스트가 있다고 가정 해 봅시다. 메소드 호출 순서는 다음과 같습니다.

setUpBeforeClass()

  (Test class first instance constructed and the following methods called on it)
    setUp()
    test1()
    tearDown()

  (Test class second instance constructed and the following methods called on it)
    setUp()
    test2()
    tearDown()

  (Test class third instance constructed and the following methods called on it)
    setUp()
    test3()
    tearDown()

tearDownAfterClass()

"BeforeClass"를 테스트 케이스의 정적 초기화 자로 생각하십시오. 정적 데이터를 초기화하는 데 사용하십시오. 테스트 케이스에서 변경되지 않습니다. 스레드로부터 안전하지 않은 정적 자원에 대해서는주의를 기울여야합니다.

마지막으로 "AfterClass"어노테이션이있는 메소드를 사용하여 "BeforeClass"어노테이션이있는 메소드에서 수행 한 설정을 정리하십시오 (자체 파괴가 충분하지 않은 경우).

"이전"및 "이후"는 단위 테스트 별 초기화를위한 것입니다. 나는 일반적으로 이러한 방법을 사용하여 종속성의 모의를 초기화 / 다시 초기화합니다. 분명히이 초기화는 단위 테스트에만 국한된 것이 아니라 모든 단위 테스트에 일반적입니다.


setUpBeforeClass는 생성자 바로 다음에 메소드 실행 전에 실행됩니다 (한 번만 실행).

각 메소드 실행 전에 설정이 실행됩니다.

tearDown is run after each method execution

tearDownAfterClass is run after all other method executions, is the last method to be executed. (run only once deconstructor)


From the Javadoc:

Sometimes several tests need to share computationally expensive setup (like logging into a database). While this can compromise the independence of tests, sometimes it is a necessary optimization. Annotating a public static void no-arg method with @BeforeClass causes it to be run once before any of the test methods in the class. The @BeforeClass methods of superclasses will be run before those the current class.

참고URL : https://stackoverflow.com/questions/3413092/difference-between-setup-and-setupbeforeclass

반응형