본문 바로가기
모바일 프로그래밍/안드로이드

Mockito Tutorial

by 프러쉬 2019. 9. 24.

Mockito는 자바에서 단위테스트를 하기 위해 Mock을 만들어주는 프레임워크
https://static.javadoc.io/org.mockito/mockito-core/2.22.0/org/mockito/Mockito.html

 

Mockito (Mockito 2.22.0 API)

Use doCallRealMethod() when you want to call the real implementation of a method. As usual you are going to read the partial mock warning: Object oriented programming is more less tackling complexity by dividing the complexity into separate, specific, SRPy

static.javadoc.io

Dependency

 // using 
 dependencies {
	testImplementation "org.mockito:mockito-core:+"
	androidTestImplementation "org.mockito:mockito-android:+"
	testImplementation "org.mockito:mockito-inline:+" // MockMaker 설정없이 사용하기 위해
 }

 

Sample

 //using mock
 mockedList.add("once");

 mockedList.add("twice");
 mockedList.add("twice");

 mockedList.add("three times");
 mockedList.add("three times");
 mockedList.add("three times");

 //following two verifications work exactly the same - times(1) is used by default
 verify(mockedList).add("once");
 verify(mockedList, times(1)).add("once");

 //exact number of invocations verification
 verify(mockedList, times(2)).add("twice");
 verify(mockedList, times(3)).add("three times");

 //verification using never(). never() is an alias to times(0)
 verify(mockedList, never()).add("never happened");

 //verification using atLeast()/atMost()
 verify(mockedList, atMostOnce()).add("once");
 verify(mockedList, atLeastOnce()).add("three times");
 verify(mockedList, atLeast(2)).add("three times");
 verify(mockedList, atMost(5)).add("three times");

댓글