Add reusable mocks.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 26 Oct 2013 16:49:26 +0000 (18:49 +0200)
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 [new file with mode: 0644]

diff --git a/src/test/java/net/pterodactylus/sone/data/Mocks.java b/src/test/java/net/pterodactylus/sone/data/Mocks.java
new file mode 100644 (file)
index 0000000..841b38e
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * Sone - Mocks.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.data;
+
+import static com.google.common.base.Optional.of;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.impl.DefaultPostBuilder;
+import net.pterodactylus.sone.database.Database;
+
+import com.google.common.base.Optional;
+
+/**
+ * Mocks reusable in multiple tests.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class Mocks {
+
+       public static Core mockCore(Database database) {
+               Core core = mock(Core.class);
+               when(core.getDatabase()).thenReturn(database);
+               when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
+               return core;
+       }
+
+       public static Database mockDatabase() {
+               Database database = mock(Database.class);
+               when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
+               return database;
+       }
+
+       public static Sone mockLocalSone(Core core, String id) {
+               Sone sone = mock(Sone.class);
+               when(sone.getId()).thenReturn(id);
+               when(sone.isLocal()).thenReturn(true);
+               Database database = core.getDatabase();
+               when(sone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
+               when(core.getSone(eq(id))).thenReturn(of(sone));
+               when(database.getSone(eq(id))).thenReturn(of(sone));
+               return sone;
+       }
+
+}