Return a useful reply builder on a mocked Sone.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 27 Oct 2013 21:15:09 +0000 (22:15 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:47 +0000 (22:25 +0100)
src/test/java/net/pterodactylus/sone/data/Mocks.java

index 841b38e..34b25f3 100644 (file)
@@ -18,6 +18,7 @@
 package net.pterodactylus.sone.data;
 
 import static com.google.common.base.Optional.of;
+import static org.mockito.ArgumentCaptor.forClass;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.mock;
@@ -25,9 +26,14 @@ import static org.mockito.Mockito.when;
 
 import net.pterodactylus.sone.core.Core;
 import net.pterodactylus.sone.data.impl.DefaultPostBuilder;
+import net.pterodactylus.sone.data.impl.DefaultPostReplyBuilder;
 import net.pterodactylus.sone.database.Database;
+import net.pterodactylus.sone.database.PostReplyBuilder;
 
 import com.google.common.base.Optional;
+import org.mockito.ArgumentCaptor;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
 
 /**
  * Mocks reusable in multiple tests.
@@ -49,12 +55,19 @@ public class Mocks {
                return database;
        }
 
-       public static Sone mockLocalSone(Core core, String id) {
+       public static Sone mockLocalSone(Core core, final String id) {
                Sone sone = mock(Sone.class);
                when(sone.getId()).thenReturn(id);
                when(sone.isLocal()).thenReturn(true);
-               Database database = core.getDatabase();
+               final Database database = core.getDatabase();
                when(sone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
+               final ArgumentCaptor<String> postIdCaptor = forClass(String.class);
+               when(sone.newPostReplyBuilder(postIdCaptor.capture())).then(new Answer<PostReplyBuilder>() {
+                       @Override
+                       public PostReplyBuilder answer(InvocationOnMock invocationOnMock) throws Throwable {
+                               return new DefaultPostReplyBuilder(database, id, postIdCaptor.getValue());
+                       }
+               });
                when(core.getSone(eq(id))).thenReturn(of(sone));
                when(database.getSone(eq(id))).thenReturn(of(sone));
                return sone;