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) {
}
+ 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;
+ }
+
+ }
+
}
@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)));
}
@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()
@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()
@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()
@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()
@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")
}
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;