Mock posts with a mocker, too.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 30 Oct 2013 06:04:14 +0000 (07:04 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:50 +0000 (22:25 +0100)
src/test/java/net/pterodactylus/sone/data/Mocks.java
src/test/java/net/pterodactylus/sone/data/SoneTest.java
src/test/java/net/pterodactylus/sone/fcp/CreateReplyCommandTest.java
src/test/java/net/pterodactylus/sone/fcp/DeletePostCommandTest.java
src/test/java/net/pterodactylus/sone/fcp/GetPostCommandTest.java

index 8720312..d5c2c08 100644 (file)
@@ -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;
+               }
+
+       }
+
 }
index 6276fcf..d54c072 100644 (file)
@@ -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)));
        }
index 7acad6f..2241eaf 100644 (file)
@@ -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.<PostReplyCreated>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.<PostReplyCreated>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.<PostReplyCreated>of(capturingPostReplyCreated));
                SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder()
index bc3cf6a..21214db 100644 (file)
@@ -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<Post> 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")
index 03dc205..c6664b9 100644 (file)
@@ -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;