Skip to content

Commit 7a61775

Browse files
committed
Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-1599-v2
2 parents 98010d0 + 8855a45 commit 7a61775

11 files changed

Lines changed: 398 additions & 1 deletion

File tree

‎guava/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@
3030
- [Guide to Guava MinMaxPriorityQueue and EvictingQueue](http://www.baeldung.com/guava-minmax-priority-queue-and-evicting-queue)
3131
- [Guide to Mathematical Utilities in Guava](http://www.baeldung.com/guava-math)
3232
- [Bloom Filter in Java using Guava](http://www.baeldung.com/guava-bloom-filter)
33+
- [Using Guava CountingOutputStream](http://www.baeldung.com/guava-counting-outputstream)

‎testing-modules/mocks/mock-comparisons/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<properties>
1616
<mockito.version>2.9.0</mockito.version>
17-
<easymock.version>3.4</easymock.version>
17+
<easymock.version>3.5.1</easymock.version>
1818
<jmockit.version>1.34</jmockit.version>
1919

2020
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.easymock;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
7+
import static java.util.stream.Collectors.toList;
8+
9+
public class ArticleReader {
10+
11+
private List<BaeldungArticle> articles;
12+
private Iterator<BaeldungArticle> articleIter;
13+
14+
public ArticleReader() {
15+
this(new ArrayList<>());
16+
}
17+
18+
public ArticleReader(List<BaeldungArticle> articles) {
19+
this.articles = articles;
20+
this.articleIter = this.articles.iterator();
21+
}
22+
23+
public List<BaeldungArticle> ofTopic(String topic) {
24+
return articles
25+
.stream()
26+
.filter(article -> article
27+
.title()
28+
.contains(topic))
29+
.collect(toList());
30+
}
31+
32+
public BaeldungArticle next() {
33+
return this.articleIter.next();
34+
}
35+
36+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.easymock;
2+
3+
public class BaeldungArticle {
4+
5+
public static BaeldungArticle simpleArticle(String title, String content) {
6+
return new BaeldungArticle(title, content);
7+
}
8+
9+
private String title;
10+
private String content;
11+
12+
private BaeldungArticle(String title, String content) {
13+
this.title = title;
14+
this.content = content;
15+
}
16+
17+
public String title() {
18+
return this.title;
19+
}
20+
21+
public String content() {
22+
return this.content;
23+
}
24+
25+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.easymock;
2+
3+
import java.util.List;
4+
5+
public class BaeldungReader {
6+
7+
private ArticleReader articleReader;
8+
private IArticleWriter articleWriter;
9+
10+
public BaeldungReader() {
11+
}
12+
13+
;
14+
15+
public BaeldungReader(ArticleReader articleReader) {
16+
this.articleReader = articleReader;
17+
}
18+
19+
public BaeldungReader(IArticleWriter writer) {
20+
this.articleWriter = writer;
21+
}
22+
23+
public BaeldungReader(ArticleReader articleReader, IArticleWriter writer) {
24+
this.articleReader = articleReader;
25+
this.articleWriter = writer;
26+
}
27+
28+
public BaeldungArticle readNext() {
29+
return articleReader.next();
30+
}
31+
32+
public List<BaeldungArticle> readTopic(String topic) {
33+
return articleReader.ofTopic(topic);
34+
}
35+
36+
public String write(String title, String content) {
37+
return articleWriter.write(title, content);
38+
}
39+
40+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.easymock;
2+
3+
public interface IArticleWriter {
4+
5+
String write(String title, String content);
6+
7+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.easymock;
2+
3+
import org.easymock.EasyMockRunner;
4+
import org.easymock.Mock;
5+
import org.easymock.TestSubject;
6+
import org.junit.*;
7+
import org.junit.runner.RunWith;
8+
9+
import java.util.NoSuchElementException;
10+
11+
import static org.easymock.EasyMock.*;
12+
13+
@RunWith(EasyMockRunner.class)
14+
public class BaeldungReaderAnnotatedTest {
15+
16+
@Mock ArticleReader mockArticleReader;
17+
18+
@Mock IArticleWriter mockArticleWriter;
19+
20+
@TestSubject BaeldungReader baeldungReader = new BaeldungReader();
21+
22+
@Test
23+
public void givenBaeldungReader_whenReadNext_thenNextArticleRead() {
24+
expect(mockArticleReader.next()).andReturn(null);
25+
replay(mockArticleReader);
26+
baeldungReader.readNext();
27+
verify(mockArticleReader);
28+
}
29+
30+
@Mock BaeldungReader mockBaeldungReader;
31+
32+
@Test
33+
public void givenBaeldungReader_whenWrite_thenWriterCalled() {
34+
expect(mockArticleWriter.write("title", "content")).andReturn(null);
35+
replay(mockArticleWriter);
36+
baeldungReader.write("title", "content");
37+
verify(mockArticleWriter);
38+
}
39+
40+
@Test
41+
public void givenArticlesInReader_whenReadTillEnd_thenThrowException() {
42+
expect(mockArticleReader.next())
43+
.andReturn(null)
44+
.times(2)
45+
.andThrow(new NoSuchElementException());
46+
replay(mockArticleReader);
47+
try {
48+
for (int i = 0; i < 3; i++) {
49+
baeldungReader.readNext();
50+
}
51+
} catch (Exception ignored) {
52+
}
53+
verify(mockArticleReader);
54+
}
55+
56+
}
57+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.easymock;
2+
3+
import org.easymock.EasyMockRule;
4+
import org.easymock.Mock;
5+
import org.easymock.TestSubject;
6+
import org.junit.*;
7+
8+
import java.util.NoSuchElementException;
9+
10+
import static org.easymock.EasyMock.*;
11+
12+
public class BaeldungReaderAnnotatedWithRuleTest {
13+
14+
@Rule public EasyMockRule mockRule = new EasyMockRule(this);
15+
16+
@Mock ArticleReader mockArticleReader;
17+
18+
@Mock IArticleWriter mockArticleWriter;
19+
20+
@TestSubject BaeldungReader baeldungReader = new BaeldungReader();
21+
22+
@Test
23+
public void givenBaeldungReader_whenReadNext_thenNextArticleRead() {
24+
expect(mockArticleReader.next()).andReturn(null);
25+
replay(mockArticleReader);
26+
baeldungReader.readNext();
27+
verify(mockArticleReader);
28+
}
29+
30+
@Mock BaeldungReader mockBaeldungReader;
31+
32+
@Test
33+
public void givenBaeldungReader_whenWrite_thenWriterCalled() {
34+
expect(mockArticleWriter.write("title", "content")).andReturn(null);
35+
replay(mockArticleWriter);
36+
baeldungReader.write("title", "content");
37+
verify(mockArticleWriter);
38+
}
39+
40+
@Test
41+
public void givenArticlesInReader_whenReadTillEnd_thenThrowException() {
42+
expect(mockArticleReader.next())
43+
.andReturn(null)
44+
.times(2)
45+
.andThrow(new NoSuchElementException());
46+
replay(mockArticleReader);
47+
try {
48+
for (int i = 0; i < 3; i++) {
49+
baeldungReader.readNext();
50+
}
51+
} catch (Exception ignored) {
52+
}
53+
verify(mockArticleReader);
54+
}
55+
56+
}
57+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.easymock;
2+
3+
import org.easymock.*;
4+
import org.junit.*;
5+
6+
import static org.easymock.EasyMock.*;
7+
8+
public class BaeldungReaderMockDelegationTest {
9+
10+
EasyMockSupport easyMockSupport = new EasyMockSupport();
11+
12+
@Test
13+
public void givenBaeldungReader_whenReadAndWrite_thenOperationSupported() {
14+
ArticleReader mockArticleReader = easyMockSupport.createMock(ArticleReader.class);
15+
IArticleWriter mockArticleWriter = easyMockSupport.createMock(IArticleWriter.class);
16+
BaeldungReader baeldungReader = new BaeldungReader(mockArticleReader, mockArticleWriter);
17+
18+
expect(mockArticleReader.next()).andReturn(null);
19+
expect(mockArticleWriter.write("title", "content")).andReturn("");
20+
easyMockSupport.replayAll();
21+
22+
baeldungReader.readNext();
23+
baeldungReader.write("title", "content");
24+
easyMockSupport.verifyAll();
25+
}
26+
27+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.easymock;
2+
3+
import org.easymock.*;
4+
import org.junit.*;
5+
import org.junit.runner.RunWith;
6+
7+
import java.util.NoSuchElementException;
8+
9+
import static org.easymock.EasyMock.*;
10+
import static org.junit.Assert.assertEquals;
11+
12+
@RunWith(EasyMockRunner.class)
13+
public class BaeldungReaderMockSupportTest extends EasyMockSupport {
14+
15+
@TestSubject BaeldungReader baeldungReader = new BaeldungReader();
16+
@Mock ArticleReader mockArticleReader;
17+
@Mock IArticleWriter mockArticleWriter;
18+
19+
@Test
20+
public void givenBaeldungReader_whenReadAndWrite_thenOperationSupported() {
21+
expect(mockArticleReader.next())
22+
.andReturn(null)
23+
.times(2)
24+
.andThrow(new NoSuchElementException());
25+
expect(mockArticleWriter.write("title", "content")).andReturn("BAEL-201801");
26+
replayAll();
27+
28+
Exception expectedException = null;
29+
try {
30+
for (int i = 0; i < 3; i++) {
31+
baeldungReader.readNext();
32+
}
33+
} catch (Exception exception) {
34+
expectedException = exception;
35+
}
36+
String articleId = baeldungReader.write("title", "content");
37+
verifyAll();
38+
assertEquals(NoSuchElementException.class, expectedException.getClass());
39+
assertEquals("BAEL-201801", articleId);
40+
}
41+
42+
}

0 commit comments

Comments
 (0)