Java Mock 테스트 도구
mockito
org.mockito
mockito-core
2.21.0
test
import org.junit.Rule;
import org.junit.Test;
import org.mockito.InOrder;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import java.util.Arrays;
import static org.mockito.Mockito.*;
public class ExampleTest {
@Rule public MockitoRule rule = MockitoJUnit.rule();
@Mock private ArticleCalculator mockCalculator;
@Mock private ArticleDatabase mockDatabase;
@InjectMocks private ArticleManager articleManager;
@Test
public void managerCountsArticlesAndSavesThemInTheDatabase() {
when(mockCalculator.countArticles("Guardian")).thenReturn(12);
when(mockCalculator.countArticlesInPolish(anyString())).thenReturn(5);
articleManager.updateArticleCounters("Guardian");
verify(mockDatabase).updateNumberOfArticles("Guardian", 12);
verify(mockDatabase).updateNumberOfPolishArticles("Guardian", 5);
verify(mockDatabase).updateNumberOfEnglishArticles("Guardian", 7);
}
@Test
public void managerCountsArticlesUsingCalculator() {
articleManager.updateArticleCounters("Guardian");
verify(mockCalculator).countArticles("Guardian");
verify(mockCalculator).countArticlesInPolish("Guardian");
}
@Test
public void managerSavesArticlesInTheDatabase() {
articleManager.updateArticleCounters("Guardian");
verify(mockDatabase).updateNumberOfArticles("Guardian", 0);
verify(mockDatabase).updateNumberOfPolishArticles("Guardian", 0);
verify(mockDatabase).updateNumberOfEnglishArticles("Guardian", 0);
}
@Test
public void managerUpdatesNumberOfRelatedArticles() {
Article articleOne = new Article();
Article articleTwo = new Article();
Article articleThree = new Article();
when(mockCalculator.countNumberOfRelatedArticles(articleOne)).thenReturn(1);
when(mockCalculator.countNumberOfRelatedArticles(articleTwo)).thenReturn(12);
when(mockCalculator.countNumberOfRelatedArticles(articleThree)).thenReturn(0);
when(mockDatabase.getArticlesFor("Guardian")).thenReturn(Arrays.asList(articleOne, articleTwo, articleThree));
articleManager.updateRelatedArticlesCounters("Guardian");
verify(mockDatabase).save(articleOne);
verify(mockDatabase).save(articleTwo);
verify(mockDatabase).save(articleThree);
}
@Test
public void shouldPersistRecalculatedArticle() {
Article articleOne = new Article();
Article articleTwo = new Article();
when(mockCalculator.countNumberOfRelatedArticles(articleOne)).thenReturn(1);
when(mockCalculator.countNumberOfRelatedArticles(articleTwo)).thenReturn(12);
when(mockDatabase.getArticlesFor("Guardian")).thenReturn(Arrays.asList(articleOne, articleTwo));
articleManager.updateRelatedArticlesCounters("Guardian");
InOrder inOrder = inOrder(mockDatabase, mockCalculator);
inOrder.verify(mockCalculator).countNumberOfRelatedArticles((Article) anyObject());
inOrder.verify(mockDatabase, atLeastOnce()).save((Article) anyObject());
}
}
EasyMock
org.easymock
easymock
3.6
test
import static org.easymock.EasyMock.*;
import org.easymock.*;
import org.junit.*;
public class ExampleTest extends EasyMockSupport {
@Rule
public EasyMockRule rule = new EasyMockRule(this);
@Mock
private Collaborator collaborator; // 1
@TestSubject
private ClassTested classUnderTest = new ClassTested(); // 2
@Test
public void addDocument() {
collaborator.documentAdded("New Document"); // 3
replayAll(); // 4
classUnderTest.addDocument("New Document", "content"); // 5
verifyAll(); // 6
}
}
PowerMock
PowerMock is a framework that extend other mock libraries such as EasyMock with more powerful capabilities. PowerMock uses a custom classloader and bytecode manipulation to enable mocking of static methods, constructors, final classes and methods, private methods, removal of static initializers and more. By using a custom classloader no changes need to be done to the IDE or continuous integration servers which simplifies adoption. Developers familiar with the supported mock frameworks will find PowerMock easy to use, since the entire expectation API is the same, both for static methods and constructors. PowerMock aims to extend the existing API's with a small number of methods and annotations to enable the extra features. Currently PowerMock supports EasyMock and Mockito.
PowerMock 프로필
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Mockito에서 결과 캡터 생성 및 사용Today I will show you how to create a Mockito captor 에는 을 사용하여 개발자가 모의 메서드 호출 중에 사용된 인수를 확인할 수 있지만 결과 자체는 확인할 수 없습니다. 사실...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.