From: David ‘Bombe’ Roden Date: Wed, 30 Oct 2013 06:04:14 +0000 (+0100) Subject: Mock posts with a mocker, too. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=1be3820cd48951abbcfd5a7f95b82d44aa0e016a;p=Sone.git Mock posts with a mocker, too. --- diff --git a/src/test/java/net/pterodactylus/sone/data/Mocks.java b/src/test/java/net/pterodactylus/sone/data/Mocks.java index 8720312..d5c2c08 100644 --- a/src/test/java/net/pterodactylus/sone/data/Mocks.java +++ b/src/test/java/net/pterodactylus/sone/data/Mocks.java @@ -84,13 +84,8 @@ public class Mocks { return new SoneMocker(id); } - public Post mockPost(Sone sone, String postId) { - Post post = mock(Post.class); - when(post.getId()).thenReturn(postId); - when(post.getSone()).thenReturn(sone); - when(database.getPost(eq(postId))).thenReturn(of(post)); - sonePosts.put(sone, post); - return post; + public PostMocker mockPost(Sone sone, String postId) { + return new PostMocker(postId, sone); } public PostReply mockPostReply(Sone sone, String replyId) { @@ -148,4 +143,25 @@ public class Mocks { } + public class PostMocker { + + private final Post post = mock(Post.class); + private final String id; + private final Sone sone; + + public PostMocker(String id, Sone sone) { + this.id = id; + this.sone = sone; + } + + public Post create() { + when(post.getId()).thenReturn(id); + when(post.getSone()).thenReturn(sone); + when(database.getPost(eq(id))).thenReturn(of(post)); + sonePosts.put(sone, post); + return post; + } + + } + } diff --git a/src/test/java/net/pterodactylus/sone/data/SoneTest.java b/src/test/java/net/pterodactylus/sone/data/SoneTest.java index 6276fcf..d54c072 100644 --- a/src/test/java/net/pterodactylus/sone/data/SoneTest.java +++ b/src/test/java/net/pterodactylus/sone/data/SoneTest.java @@ -37,11 +37,11 @@ public class SoneTest { @Test public void verifyThatTransformingASoneIntoItsPostsWorks() { Sone sone = mocks.mockSone("Sone").local().create(); - Post post1 = mocks.mockPost(sone, "Post1"); + Post post1 = mocks.mockPost(sone, "Post1").create(); when(post1.getTime()).thenReturn(1000L); - Post post2 = mocks.mockPost(sone, "Post2"); + Post post2 = mocks.mockPost(sone, "Post2").create(); when(post2.getTime()).thenReturn(2000L); - Post post3 = mocks.mockPost(sone, "Post3"); + Post post3 = mocks.mockPost(sone, "Post3").create(); when(post3.getTime()).thenReturn(3000L); assertThat(TO_POSTS.apply(sone), contains(is(post3), is(post2), is(post1))); } diff --git a/src/test/java/net/pterodactylus/sone/fcp/CreateReplyCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/CreateReplyCommandTest.java index 7acad6f..2241eaf 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/CreateReplyCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/CreateReplyCommandTest.java @@ -54,7 +54,7 @@ public class CreateReplyCommandTest { @Test public void verifyThatCreatingAFullySpecifiedReplyWorks() throws FcpException { Sone sone = mocks.mockSone("SoneId").local().create(); - mocks.mockPost(sone, "PostId"); + mocks.mockPost(sone, "PostId").create(); CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated(); when(mocks.core.postReplyCreated()).thenReturn(Optional.of(capturingPostReplyCreated)); SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder() @@ -79,7 +79,7 @@ public class CreateReplyCommandTest { @Test(expected = FcpException.class) public void verifyThatCreatingAReplyWithoutSoneCausesAnError() throws FcpException { Sone sone = mocks.mockSone("SoneId").local().create(); - mocks.mockPost(sone, "PostId"); + mocks.mockPost(sone, "PostId").create(); CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated(); when(mocks.core.postReplyCreated()).thenReturn(Optional.of(capturingPostReplyCreated)); SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder() @@ -106,7 +106,7 @@ public class CreateReplyCommandTest { @Test(expected = FcpException.class) public void verifyThatCreatingAReplyWithoutTextCausesAnError() throws FcpException { Sone sone = mocks.mockSone("SoneId").local().create(); - mocks.mockPost(sone, "PostId"); + mocks.mockPost(sone, "PostId").create(); CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated(); when(mocks.core.postReplyCreated()).thenReturn(Optional.of(capturingPostReplyCreated)); SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder() diff --git a/src/test/java/net/pterodactylus/sone/fcp/DeletePostCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/DeletePostCommandTest.java index bc3cf6a..21214db 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/DeletePostCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/DeletePostCommandTest.java @@ -49,7 +49,7 @@ public class DeletePostCommandTest { @Test public void verifyThatDeletingAPostWorks() throws FcpException { Sone sone = mocks.mockSone("Sone").local().create(); - Post post = mocks.mockPost(sone, "PostId"); + Post post = mocks.mockPost(sone, "PostId").create(); ArgumentCaptor deletedPost = forClass(Post.class); doNothing().when(mocks.core).deletePost(deletedPost.capture()); SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder() @@ -66,7 +66,7 @@ public class DeletePostCommandTest { @Test public void verifyThatDeletingAPostFromANonLocalSoneCausesAnError() throws FcpException { Sone sone = mocks.mockSone("Sone").create(); - Post post = mocks.mockPost(sone, "PostId"); + Post post = mocks.mockPost(sone, "PostId").create(); SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder() .put("Message", "DeletePost") .put("Post", "PostId") diff --git a/src/test/java/net/pterodactylus/sone/fcp/GetPostCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/GetPostCommandTest.java index 03dc205..c6664b9 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/GetPostCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/GetPostCommandTest.java @@ -132,7 +132,7 @@ public class GetPostCommandTest { } private Post preparePost(Sone sone) { - Post post = mocks.mockPost(sone, "PostId"); + Post post = mocks.mockPost(sone, "PostId").create(); when(post.getText()).thenReturn("Text of the post."); when(post.getTime()).thenReturn(1000L); return post;