From: David ‘Bombe’ Roden Date: Sat, 26 Oct 2013 16:49:26 +0000 (+0200) Subject: Add reusable mocks. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=0c5f5e59b4902f2d988fc191b22b81831f2a1aad;p=Sone.git Add reusable mocks. --- 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 index 0000000..841b38e --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/data/Mocks.java @@ -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 . + */ + +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 David ‘Bombe’ Roden + */ +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.absent()); + return core; + } + + public static Database mockDatabase() { + Database database = mock(Database.class); + when(database.getSone(anyString())).thenReturn(Optional.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; + } + +}