강좌/TDD & Test

Mockito 002 reset

여름나라겨울이야기 2013. 4. 29. 18:02
728x90

Mockito 001 에 이어서


1. 테스트 클래스 추가

package paradise;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class CalculatorResetTest {
	private static Calculator mockCalculator;
	
	@BeforeClass
	public static void setUpBeforeClass() {
		mockCalculator = mock(Calculator.class);
	}
	
	@Before
	public void setUp() {
		reset(mockCalculator);
	}
	
	@Test
	public void testAdd() {
		int int1 = 10;
		int int2 = 20;
		
		when(mockCalculator.add(int1, int2)).thenReturn(int1 + int2);

		assertThat(int1 + int2, is(mockCalculator.add(int1, int2)));
		assertThat(30, is(mockCalculator.add(int1, int2)));
		
		verify(mockCalculator, times(2)).add(int1, int2);
		System.out.println(mockCalculator.add(int1, int2));
	}
	
	@Test
	public void testAdd2() {
		int int1 = 1;
		int int2 = 2;
		
		when(mockCalculator.add(int1, int2)).thenReturn(int1 + int2);

		assertThat(int1 + int2, is(mockCalculator.add(int1, int2)));
		assertThat(3, is(mockCalculator.add(int1, int2)));
		
		verify(mockCalculator, times(2)).add(int1, int2);
		System.out.println(mockCalculator.add(int1, int2));
	}
}


cf) 25 라인에서 reset 해주지 않으면 두 개의 테스트 사이에서 Mockito 상태가 공유됨


반응형

'강좌 > TDD & Test' 카테고리의 다른 글

[Spock] 설치 구성 및 확인  (0) 2015.01.06
Mockito 001  (0) 2013.04.29
Spring MVC Test 001  (0) 2013.04.26