강좌/TDD & Test

Mockito 001

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

1. File > New > Projects... > Maven > Maven Project 

   Next 클릭



   Next 클릭



   Finish 클릭


2.  클래스 구현

package paradise;

public class Calculator {
	public int add(int a, int b) {
		return a + b;
	}
}


3. POM.xml 에 dependency 추가 


	4.0.0
	com.heaven
	mockito001
	0.0.1-SNAPSHOT
	
		
		
			junit
			junit
			4.7
			test
		
		
			org.mockito
			mockito-core
			1.8.5
			jar
			test
		
	


4. JUnit Test Case 구현

package paradise;

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

import org.junit.Test;

public class CalculatorTest {
	@Test
	public void testAdd() {
		int int1 = 10;
		int int2 = 20;
		
		Calculator mockCalculator = mock(Calculator.class);
		when(mockCalculator.add(int1, int2)).thenReturn(int1 + int2);
		
		assertThat(int1 + int2, is(mockCalculator.add(int1, int2)));
		
		verify(mockCalculator, times(1)).add(int1, int2);
	}
}


5. Context Menu > Run as > JUnit Test




cf) Concrete Class 보다는 interface 를 통한 테스트 강츄!!!

반응형

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

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