BAEL-1565 (#3869)
* BAEL-1565 * move BAEL-1565 code to testing-modules/mocks/mock-comparisons
This commit is contained in:
parent
26b94ac17e
commit
8855a45ddf
|
@ -14,7 +14,7 @@
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<mockito.version>2.9.0</mockito.version>
|
<mockito.version>2.9.0</mockito.version>
|
||||||
<easymock.version>3.4</easymock.version>
|
<easymock.version>3.5.1</easymock.version>
|
||||||
<jmockit.version>1.34</jmockit.version>
|
<jmockit.version>1.34</jmockit.version>
|
||||||
|
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
package com.baeldung.easymock;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static java.util.stream.Collectors.toList;
|
||||||
|
|
||||||
|
public class ArticleReader {
|
||||||
|
|
||||||
|
private List<BaeldungArticle> articles;
|
||||||
|
private Iterator<BaeldungArticle> articleIter;
|
||||||
|
|
||||||
|
public ArticleReader() {
|
||||||
|
this(new ArrayList<>());
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArticleReader(List<BaeldungArticle> articles) {
|
||||||
|
this.articles = articles;
|
||||||
|
this.articleIter = this.articles.iterator();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<BaeldungArticle> ofTopic(String topic) {
|
||||||
|
return articles
|
||||||
|
.stream()
|
||||||
|
.filter(article -> article
|
||||||
|
.title()
|
||||||
|
.contains(topic))
|
||||||
|
.collect(toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
public BaeldungArticle next() {
|
||||||
|
return this.articleIter.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
package com.baeldung.easymock;
|
||||||
|
|
||||||
|
public class BaeldungArticle {
|
||||||
|
|
||||||
|
public static BaeldungArticle simpleArticle(String title, String content) {
|
||||||
|
return new BaeldungArticle(title, content);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String title;
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
private BaeldungArticle(String title, String content) {
|
||||||
|
this.title = title;
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String title() {
|
||||||
|
return this.title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String content() {
|
||||||
|
return this.content;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
package com.baeldung.easymock;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class BaeldungReader {
|
||||||
|
|
||||||
|
private ArticleReader articleReader;
|
||||||
|
private IArticleWriter articleWriter;
|
||||||
|
|
||||||
|
public BaeldungReader() {
|
||||||
|
}
|
||||||
|
|
||||||
|
;
|
||||||
|
|
||||||
|
public BaeldungReader(ArticleReader articleReader) {
|
||||||
|
this.articleReader = articleReader;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BaeldungReader(IArticleWriter writer) {
|
||||||
|
this.articleWriter = writer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BaeldungReader(ArticleReader articleReader, IArticleWriter writer) {
|
||||||
|
this.articleReader = articleReader;
|
||||||
|
this.articleWriter = writer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BaeldungArticle readNext() {
|
||||||
|
return articleReader.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<BaeldungArticle> readTopic(String topic) {
|
||||||
|
return articleReader.ofTopic(topic);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String write(String title, String content) {
|
||||||
|
return articleWriter.write(title, content);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package com.baeldung.easymock;
|
||||||
|
|
||||||
|
public interface IArticleWriter {
|
||||||
|
|
||||||
|
String write(String title, String content);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,57 @@
|
||||||
|
package com.baeldung.easymock;
|
||||||
|
|
||||||
|
import org.easymock.EasyMockRunner;
|
||||||
|
import org.easymock.Mock;
|
||||||
|
import org.easymock.TestSubject;
|
||||||
|
import org.junit.*;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
|
import static org.easymock.EasyMock.*;
|
||||||
|
|
||||||
|
@RunWith(EasyMockRunner.class)
|
||||||
|
public class BaeldungReaderAnnotatedTest {
|
||||||
|
|
||||||
|
@Mock ArticleReader mockArticleReader;
|
||||||
|
|
||||||
|
@Mock IArticleWriter mockArticleWriter;
|
||||||
|
|
||||||
|
@TestSubject BaeldungReader baeldungReader = new BaeldungReader();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBaeldungReader_whenReadNext_thenNextArticleRead() {
|
||||||
|
expect(mockArticleReader.next()).andReturn(null);
|
||||||
|
replay(mockArticleReader);
|
||||||
|
baeldungReader.readNext();
|
||||||
|
verify(mockArticleReader);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Mock BaeldungReader mockBaeldungReader;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBaeldungReader_whenWrite_thenWriterCalled() {
|
||||||
|
expect(mockArticleWriter.write("title", "content")).andReturn(null);
|
||||||
|
replay(mockArticleWriter);
|
||||||
|
baeldungReader.write("title", "content");
|
||||||
|
verify(mockArticleWriter);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenArticlesInReader_whenReadTillEnd_thenThrowException() {
|
||||||
|
expect(mockArticleReader.next())
|
||||||
|
.andReturn(null)
|
||||||
|
.times(2)
|
||||||
|
.andThrow(new NoSuchElementException());
|
||||||
|
replay(mockArticleReader);
|
||||||
|
try {
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
baeldungReader.readNext();
|
||||||
|
}
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
}
|
||||||
|
verify(mockArticleReader);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,57 @@
|
||||||
|
package com.baeldung.easymock;
|
||||||
|
|
||||||
|
import org.easymock.EasyMockRule;
|
||||||
|
import org.easymock.Mock;
|
||||||
|
import org.easymock.TestSubject;
|
||||||
|
import org.junit.*;
|
||||||
|
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
|
import static org.easymock.EasyMock.*;
|
||||||
|
|
||||||
|
public class BaeldungReaderAnnotatedWithRuleTest {
|
||||||
|
|
||||||
|
@Rule public EasyMockRule mockRule = new EasyMockRule(this);
|
||||||
|
|
||||||
|
@Mock ArticleReader mockArticleReader;
|
||||||
|
|
||||||
|
@Mock IArticleWriter mockArticleWriter;
|
||||||
|
|
||||||
|
@TestSubject BaeldungReader baeldungReader = new BaeldungReader();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBaeldungReader_whenReadNext_thenNextArticleRead() {
|
||||||
|
expect(mockArticleReader.next()).andReturn(null);
|
||||||
|
replay(mockArticleReader);
|
||||||
|
baeldungReader.readNext();
|
||||||
|
verify(mockArticleReader);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Mock BaeldungReader mockBaeldungReader;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBaeldungReader_whenWrite_thenWriterCalled() {
|
||||||
|
expect(mockArticleWriter.write("title", "content")).andReturn(null);
|
||||||
|
replay(mockArticleWriter);
|
||||||
|
baeldungReader.write("title", "content");
|
||||||
|
verify(mockArticleWriter);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenArticlesInReader_whenReadTillEnd_thenThrowException() {
|
||||||
|
expect(mockArticleReader.next())
|
||||||
|
.andReturn(null)
|
||||||
|
.times(2)
|
||||||
|
.andThrow(new NoSuchElementException());
|
||||||
|
replay(mockArticleReader);
|
||||||
|
try {
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
baeldungReader.readNext();
|
||||||
|
}
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
}
|
||||||
|
verify(mockArticleReader);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.baeldung.easymock;
|
||||||
|
|
||||||
|
import org.easymock.*;
|
||||||
|
import org.junit.*;
|
||||||
|
|
||||||
|
import static org.easymock.EasyMock.*;
|
||||||
|
|
||||||
|
public class BaeldungReaderMockDelegationTest {
|
||||||
|
|
||||||
|
EasyMockSupport easyMockSupport = new EasyMockSupport();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBaeldungReader_whenReadAndWrite_thenOperationSupported() {
|
||||||
|
ArticleReader mockArticleReader = easyMockSupport.createMock(ArticleReader.class);
|
||||||
|
IArticleWriter mockArticleWriter = easyMockSupport.createMock(IArticleWriter.class);
|
||||||
|
BaeldungReader baeldungReader = new BaeldungReader(mockArticleReader, mockArticleWriter);
|
||||||
|
|
||||||
|
expect(mockArticleReader.next()).andReturn(null);
|
||||||
|
expect(mockArticleWriter.write("title", "content")).andReturn("");
|
||||||
|
easyMockSupport.replayAll();
|
||||||
|
|
||||||
|
baeldungReader.readNext();
|
||||||
|
baeldungReader.write("title", "content");
|
||||||
|
easyMockSupport.verifyAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,42 @@
|
||||||
|
package com.baeldung.easymock;
|
||||||
|
|
||||||
|
import org.easymock.*;
|
||||||
|
import org.junit.*;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
|
import static org.easymock.EasyMock.*;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
@RunWith(EasyMockRunner.class)
|
||||||
|
public class BaeldungReaderMockSupportTest extends EasyMockSupport {
|
||||||
|
|
||||||
|
@TestSubject BaeldungReader baeldungReader = new BaeldungReader();
|
||||||
|
@Mock ArticleReader mockArticleReader;
|
||||||
|
@Mock IArticleWriter mockArticleWriter;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBaeldungReader_whenReadAndWrite_thenOperationSupported() {
|
||||||
|
expect(mockArticleReader.next())
|
||||||
|
.andReturn(null)
|
||||||
|
.times(2)
|
||||||
|
.andThrow(new NoSuchElementException());
|
||||||
|
expect(mockArticleWriter.write("title", "content")).andReturn("BAEL-201801");
|
||||||
|
replayAll();
|
||||||
|
|
||||||
|
Exception expectedException = null;
|
||||||
|
try {
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
baeldungReader.readNext();
|
||||||
|
}
|
||||||
|
} catch (Exception exception) {
|
||||||
|
expectedException = exception;
|
||||||
|
}
|
||||||
|
String articleId = baeldungReader.write("title", "content");
|
||||||
|
verifyAll();
|
||||||
|
assertEquals(NoSuchElementException.class, expectedException.getClass());
|
||||||
|
assertEquals("BAEL-201801", articleId);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,105 @@
|
||||||
|
package com.baeldung.easymock;
|
||||||
|
|
||||||
|
import org.junit.*;
|
||||||
|
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
|
import static org.easymock.EasyMock.*;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class BaeldungReaderTest {
|
||||||
|
|
||||||
|
private BaeldungReader baeldungReader;
|
||||||
|
|
||||||
|
private ArticleReader mockArticleReader;
|
||||||
|
|
||||||
|
private IArticleWriter mockArticleWriter;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBaeldungReader_whenReadNext_thenNextArticleRead() {
|
||||||
|
mockArticleReader = mock(ArticleReader.class);
|
||||||
|
baeldungReader = new BaeldungReader(mockArticleReader);
|
||||||
|
|
||||||
|
expect(mockArticleReader.next()).andReturn(null);
|
||||||
|
replay(mockArticleReader);
|
||||||
|
|
||||||
|
BaeldungArticle article = baeldungReader.readNext();
|
||||||
|
verify(mockArticleReader);
|
||||||
|
assertEquals(null, article);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBaeldungReader_whenReadNextAndSkimTopics_thenAllAllowed() {
|
||||||
|
mockArticleReader = strictMock(ArticleReader.class);
|
||||||
|
baeldungReader = new BaeldungReader(mockArticleReader);
|
||||||
|
|
||||||
|
expect(mockArticleReader.next()).andReturn(null);
|
||||||
|
expect(mockArticleReader.ofTopic("easymock")).andReturn(null);
|
||||||
|
replay(mockArticleReader);
|
||||||
|
|
||||||
|
baeldungReader.readNext();
|
||||||
|
baeldungReader.readTopic("easymock");
|
||||||
|
verify(mockArticleReader);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBaeldungReader_whenReadNextAndOthers_thenAllowed() {
|
||||||
|
mockArticleReader = niceMock(ArticleReader.class);
|
||||||
|
baeldungReader = new BaeldungReader(mockArticleReader);
|
||||||
|
|
||||||
|
expect(mockArticleReader.next()).andReturn(null);
|
||||||
|
replay(mockArticleReader);
|
||||||
|
|
||||||
|
baeldungReader.readNext();
|
||||||
|
baeldungReader.readTopic("easymock");
|
||||||
|
verify(mockArticleReader);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBaeldungReader_whenWriteMaliciousContent_thenArgumentIllegal() {
|
||||||
|
mockArticleWriter = mock(IArticleWriter.class);
|
||||||
|
baeldungReader = new BaeldungReader(mockArticleWriter);
|
||||||
|
expect(mockArticleWriter.write("easymock", "<body onload=alert('baeldung')>")).andThrow(new IllegalArgumentException());
|
||||||
|
replay(mockArticleWriter);
|
||||||
|
|
||||||
|
Exception expectedException = null;
|
||||||
|
try {
|
||||||
|
baeldungReader.write("easymock", "<body onload=alert('baeldung')>");
|
||||||
|
} catch (Exception exception) {
|
||||||
|
expectedException = exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
verify(mockArticleWriter);
|
||||||
|
assertEquals(IllegalArgumentException.class, expectedException.getClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBaeldungReader_whenWrite_thenWriterCalled() {
|
||||||
|
mockArticleWriter = mock(IArticleWriter.class);
|
||||||
|
baeldungReader = new BaeldungReader(mockArticleWriter);
|
||||||
|
expect(mockArticleWriter.write("title", "content")).andReturn(null);
|
||||||
|
replay(mockArticleWriter);
|
||||||
|
String articleId = baeldungReader.write("title", "content");
|
||||||
|
verify(mockArticleWriter);
|
||||||
|
assertEquals(null, articleId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenArticlesInReader_whenReadTillEnd_thenThrowException() {
|
||||||
|
ArticleReader mockArticleReader = mock(ArticleReader.class);
|
||||||
|
baeldungReader = new BaeldungReader(mockArticleReader);
|
||||||
|
expect(mockArticleReader.next())
|
||||||
|
.andReturn(null)
|
||||||
|
.times(2)
|
||||||
|
.andThrow(new NoSuchElementException());
|
||||||
|
replay(mockArticleReader);
|
||||||
|
try {
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
baeldungReader.readNext();
|
||||||
|
}
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
}
|
||||||
|
verify(mockArticleReader);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue