From 8855a45ddf1936b698980b36736e455aad4be5ef Mon Sep 17 00:00:00 2001 From: aietcn Date: Sun, 25 Mar 2018 01:36:23 +0800 Subject: [PATCH] BAEL-1565 (#3869) * BAEL-1565 * move BAEL-1565 code to testing-modules/mocks/mock-comparisons --- .../mocks/mock-comparisons/pom.xml | 2 +- .../com/baeldung/easymock/ArticleReader.java | 36 ++++++ .../baeldung/easymock/BaeldungArticle.java | 25 +++++ .../com/baeldung/easymock/BaeldungReader.java | 40 +++++++ .../com/baeldung/easymock/IArticleWriter.java | 7 ++ .../easymock/BaeldungReaderAnnotatedTest.java | 57 ++++++++++ .../BaeldungReaderAnnotatedWithRuleTest.java | 57 ++++++++++ .../BaeldungReaderMockDelegationTest.java | 27 +++++ .../BaeldungReaderMockSupportTest.java | 42 +++++++ .../baeldung/easymock/BaeldungReaderTest.java | 105 ++++++++++++++++++ 10 files changed, 397 insertions(+), 1 deletion(-) create mode 100755 testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/ArticleReader.java create mode 100755 testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/BaeldungArticle.java create mode 100755 testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/BaeldungReader.java create mode 100755 testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/IArticleWriter.java create mode 100755 testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderAnnotatedTest.java create mode 100755 testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderAnnotatedWithRuleTest.java create mode 100755 testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderMockDelegationTest.java create mode 100755 testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderMockSupportTest.java create mode 100755 testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderTest.java diff --git a/testing-modules/mocks/mock-comparisons/pom.xml b/testing-modules/mocks/mock-comparisons/pom.xml index 84f1d20401..5c1d5d31cd 100644 --- a/testing-modules/mocks/mock-comparisons/pom.xml +++ b/testing-modules/mocks/mock-comparisons/pom.xml @@ -14,7 +14,7 @@ 2.9.0 - 3.4 + 3.5.1 1.34 UTF-8 diff --git a/testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/ArticleReader.java b/testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/ArticleReader.java new file mode 100755 index 0000000000..9e4a15c27a --- /dev/null +++ b/testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/ArticleReader.java @@ -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 articles; + private Iterator articleIter; + + public ArticleReader() { + this(new ArrayList<>()); + } + + public ArticleReader(List articles) { + this.articles = articles; + this.articleIter = this.articles.iterator(); + } + + public List ofTopic(String topic) { + return articles + .stream() + .filter(article -> article + .title() + .contains(topic)) + .collect(toList()); + } + + public BaeldungArticle next() { + return this.articleIter.next(); + } + +} \ No newline at end of file diff --git a/testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/BaeldungArticle.java b/testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/BaeldungArticle.java new file mode 100755 index 0000000000..f386c980f4 --- /dev/null +++ b/testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/BaeldungArticle.java @@ -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; + } + +} \ No newline at end of file diff --git a/testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/BaeldungReader.java b/testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/BaeldungReader.java new file mode 100755 index 0000000000..319dfc5d77 --- /dev/null +++ b/testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/BaeldungReader.java @@ -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 readTopic(String topic) { + return articleReader.ofTopic(topic); + } + + public String write(String title, String content) { + return articleWriter.write(title, content); + } + +} \ No newline at end of file diff --git a/testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/IArticleWriter.java b/testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/IArticleWriter.java new file mode 100755 index 0000000000..4c8b190464 --- /dev/null +++ b/testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/IArticleWriter.java @@ -0,0 +1,7 @@ +package com.baeldung.easymock; + +public interface IArticleWriter { + + String write(String title, String content); + +} \ No newline at end of file diff --git a/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderAnnotatedTest.java b/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderAnnotatedTest.java new file mode 100755 index 0000000000..af2ef3e6da --- /dev/null +++ b/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderAnnotatedTest.java @@ -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); + } + +} + diff --git a/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderAnnotatedWithRuleTest.java b/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderAnnotatedWithRuleTest.java new file mode 100755 index 0000000000..28ed1484fc --- /dev/null +++ b/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderAnnotatedWithRuleTest.java @@ -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); + } + +} + diff --git a/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderMockDelegationTest.java b/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderMockDelegationTest.java new file mode 100755 index 0000000000..f3cff97d47 --- /dev/null +++ b/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderMockDelegationTest.java @@ -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(); + } + +} \ No newline at end of file diff --git a/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderMockSupportTest.java b/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderMockSupportTest.java new file mode 100755 index 0000000000..bc3a953d06 --- /dev/null +++ b/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderMockSupportTest.java @@ -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); + } + +} \ No newline at end of file diff --git a/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderTest.java b/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderTest.java new file mode 100755 index 0000000000..5b485357fd --- /dev/null +++ b/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderTest.java @@ -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", "")).andThrow(new IllegalArgumentException()); + replay(mockArticleWriter); + + Exception expectedException = null; + try { + baeldungReader.write("easymock", ""); + } 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); + } + +} \ No newline at end of file