Test case refactoring, test doUpdate

Boundary Values are awesome - https://www.destroyallsoftware.com/talks/boundaries
This commit is contained in:
Bradlee Speice 2014-10-22 23:32:18 -04:00
parent f4e8ffaebc
commit 8104ffa862

View File

@ -14,6 +14,7 @@ import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
@ -34,6 +35,7 @@ import static org.mockito.Mockito.when;
public class RefreshManagerTest extends MBTestCase implements Injector { public class RefreshManagerTest extends MBTestCase implements Injector {
final String mockBookName = "MockBook";
/** /**
* The object graph that should be given to classes under test. Each test is responsible * The object graph that should be given to classes under test. Each test is responsible
* for setting their own ObjectGraph. * for setting their own ObjectGraph.
@ -41,20 +43,15 @@ public class RefreshManagerTest extends MBTestCase implements Injector {
ObjectGraph mObjectGraph; ObjectGraph mObjectGraph;
@Inject @Inject
RefreshManager rM; RefreshManager rM;
Installer mockInstaller;
Book mockBook;
@Override public void setUp() {
public void inject(Object o) {
mObjectGraph.inject(o);
}
public void testGetAvailableModulesFlattened() throws Exception {
// Environment setup // Environment setup
final String mockBookName = "MockBook"; mockBook = mock(Book.class);
Book mockBook = mock(Book.class);
when(mockBook.getName()).thenReturn(mockBookName); when(mockBook.getName()).thenReturn(mockBookName);
Installer mockInstaller = mock(Installer.class); mockInstaller = mock(Installer.class);
List<Book> bookList = new ArrayList<Book>(); List<Book> bookList = new ArrayList<Book>();
bookList.add(mockBook); bookList.add(mockBook);
when(mockInstaller.getBooks()).thenReturn(bookList); when(mockInstaller.getBooks()).thenReturn(bookList);
@ -67,7 +64,14 @@ public class RefreshManagerTest extends MBTestCase implements Injector {
// Now the actual test // Now the actual test
mObjectGraph.inject(this); // Get the RefreshManager mObjectGraph.inject(this); // Get the RefreshManager
}
@Override
public void inject(Object o) {
mObjectGraph.inject(o);
}
public void testGetAvailableModulesFlattened() throws Exception {
rM.getAvailableModulesFlat() rM.getAvailableModulesFlat()
.toBlocking() .toBlocking()
.forEach(new Action1<Book>() { .forEach(new Action1<Book>() {
@ -82,22 +86,6 @@ public class RefreshManagerTest extends MBTestCase implements Injector {
} }
public void testInstallerFromBook() throws Exception { public void testInstallerFromBook() throws Exception {
// Environment setup
Book mockBook = mock(Book.class);
Installer mockInstaller = mock(Installer.class);
List<Book> bookList = new ArrayList<Book>();
bookList.add(mockBook);
when(mockInstaller.getBooks()).thenReturn(bookList);
Collection<Installer> mockInstallers = new ArrayList<Installer>();
mockInstallers.add(mockInstaller);
RMTModules modules = new RMTModules(mockInstallers);
mObjectGraph = ObjectGraph.create(modules);
// And the actual test
mObjectGraph.inject(this);
Installer i = rM.installerFromBook(mockBook).toBlocking().first(); Installer i = rM.installerFromBook(mockBook).toBlocking().first();
assertSame(mockInstaller, i); assertSame(mockInstaller, i);
@ -105,7 +93,7 @@ public class RefreshManagerTest extends MBTestCase implements Injector {
} }
public void testRefreshSeparateThread() { public void testRefreshSeparateThread() {
Installer mockInstaller = mock(Installer.class); mockInstaller = mock(Installer.class);
final List<Book> bookList = new ArrayList<Book>(); final List<Book> bookList = new ArrayList<Book>();
when(mockInstaller.getBooks()).thenAnswer(new Answer<List<Book>>() { when(mockInstaller.getBooks()).thenAnswer(new Answer<List<Book>>() {
@Override @Override
@ -137,6 +125,26 @@ public class RefreshManagerTest extends MBTestCase implements Injector {
}); });
} }
/**
* Test the conditions are right for downloading
* I'd like to point out that I can test all of this without requiring mocking of
* either the preferences or network state. Value Boundaries for the win.
*/
public void testDoUpdate() {
long fourteenDaysAgo = Calendar.getInstance().getTime().getTime() - 1209600;
long sixteenDaysAgo = Calendar.getInstance().getTime().getTime() - 1382400;
assertFalse(rM.doReload(true, fourteenDaysAgo, false));
assertFalse(rM.doReload(true, fourteenDaysAgo, true));
assertFalse(rM.doReload(true, sixteenDaysAgo, false));
assertTrue(rM.doReload(true, sixteenDaysAgo, true));
assertFalse(rM.doReload(false, fourteenDaysAgo, true));
assertFalse(rM.doReload(false, fourteenDaysAgo, false));
assertFalse(rM.doReload(false, sixteenDaysAgo, true));
assertFalse(rM.doReload(false, sixteenDaysAgo, false));
}
@Module(injects = {RefreshManagerTest.class, RefreshManager.class}) @Module(injects = {RefreshManagerTest.class, RefreshManager.class})
@SuppressWarnings("unused") @SuppressWarnings("unused")
class RMTModules { class RMTModules {