Skip to content

Commit

Permalink
Replace Hamcrest with Truth.
Browse files Browse the repository at this point in the history
Fewer static imports, better wrangling of generics
(particularly when asserting contains), and, least
of all, better internal support.
  • Loading branch information
sjudd committed Nov 3, 2014
1 parent f4d511e commit 8588abe
Show file tree
Hide file tree
Showing 32 changed files with 143 additions and 188 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ JUNIT_VERSION=4.11
MOCKITO_VERSION=1.9.5
ROBOLECTRIC_VERSION=2.4-SNAPSHOT
MOCKWEBSERVER_VERSION=1.6.0
HAMCREST_VERSION=1.3
TRUTH_VERSION=0.24

COMPILE_SDK_VERSION=19
BUILD_TOOLS_VERSION=19.1.0
Expand Down
3 changes: 1 addition & 2 deletions integration/volley/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ dependencies {
compile project(':glide')
compile "com.mcxiaoke.volley:library:${VOLLEY_VERSION}"

androidTestCompile "org.hamcrest:hamcrest-core:${HAMCREST_VERSION}"
androidTestCompile "org.hamcrest:hamcrest-library:${HAMCREST_VERSION}"
androidTestCompile "com.google.truth:truth:${TRUTH_VERSION}"
androidTestCompile "junit:junit:${JUNIT_VERSION}"
androidTestCompile "org.mockito:mockito-all:${MOCKITO_VERSION}"
androidTestCompile "org.robolectric:robolectric:${ROBOLECTRIC_VERSION}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@
import java.net.URL;
import java.util.concurrent.ExecutionException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
Expand Down Expand Up @@ -67,7 +65,7 @@ public void testReturnsInputStreamOnStatusOk() throws Exception {
.setResponseCode(200));
DataFetcher<InputStream> fetcher = getFetcher();
InputStream is = fetcher.loadData(Priority.HIGH);
assertThat(isToString(is), equalTo(expected));
assertEquals(expected, isToString(is));
}

@Test
Expand All @@ -80,7 +78,7 @@ public void testHandlesRedirect301s() throws Exception {
.setResponseCode(200)
.setBody(expected));
InputStream is = getFetcher().loadData(Priority.LOW);
assertThat(isToString(is), equalTo(expected));
assertEquals(expected, isToString(is));
}

@Test
Expand All @@ -93,7 +91,7 @@ public void testHandlesRedirect302s() throws Exception {
.setResponseCode(200)
.setBody(expected));
InputStream is = getFetcher().loadData(Priority.LOW);
assertThat(isToString(is), equalTo(expected));
assertEquals(expected, isToString(is));
}

@Test
Expand All @@ -110,11 +108,11 @@ public void testHandlesUpToFiveRedirects() throws Exception {
.setResponseCode(200).setBody(expected));

InputStream is = getFetcher().loadData(Priority.NORMAL);
assertThat(isToString(is), equalTo(expected));
assertEquals(expected, isToString(is));

assertThat(mockWebServer.takeRequest().getPath(), containsString(DEFAULT_PATH));
assertThat(mockWebServer.takeRequest().getPath()).contains(DEFAULT_PATH);
for (int i = 0; i < numRedirects; i++) {
assertThat(mockWebServer.takeRequest().getPath(), containsString(redirectBase + i));
assertThat(mockWebServer.takeRequest().getPath()).contains(redirectBase + i);
}
}

Expand All @@ -128,7 +126,7 @@ public void testThrowsIfRedirectLocationIsEmpty() throws Exception {
getFetcher().loadData(Priority.NORMAL);
fail("Didn't get expected IOException");
} catch (ExecutionException e) {
assertThat(e.getCause(), instanceOf(VolleyError.class));
assertThat(e.getCause()).isInstanceOf(VolleyError.class);
}
}

Expand All @@ -139,7 +137,7 @@ public void testThrowsIfStatusCodeIsNegativeOne() throws Exception {
getFetcher().loadData(Priority.LOW);
fail("Failed to get expected exception");
} catch (ExecutionException e) {
assertThat(e.getCause(), instanceOf(NoConnectionError.class));
assertThat(e.getCause()).isInstanceOf(NoConnectionError.class);
}
}

Expand All @@ -154,8 +152,8 @@ public void testThrowsAfterTooManyRedirects() throws Exception {
getFetcher().loadData(Priority.NORMAL);
fail("Failed to get expected exception");
} catch (ExecutionException e) {
assertThat(e.getCause(), instanceOf(NoConnectionError.class));
assertThat(e.getCause().getCause(), instanceOf(ProtocolException.class));
assertThat(e.getCause()).isInstanceOf(NoConnectionError.class);
assertThat(e.getCause().getCause()).isInstanceOf(ProtocolException.class);
}
}

Expand All @@ -167,7 +165,7 @@ public void testThrowsIfStatusCodeIs500() throws Exception {
getFetcher().loadData(Priority.NORMAL);
fail("Failed to get expected exception");
} catch (ExecutionException e) {
assertThat(e.getCause(), instanceOf(ServerError.class));
assertThat(e.getCause()).isInstanceOf(ServerError.class);
}
}

Expand All @@ -178,7 +176,7 @@ public void testThrowsIfStatusCodeIs400() throws Exception {
getFetcher().loadData(Priority.LOW);
fail("Failed to get expected exception");
} catch (ExecutionException e) {
assertThat(e.getCause(), instanceOf(ServerError.class));
assertThat(e.getCause()).isInstanceOf(ServerError.class);
}
}

Expand Down
3 changes: 1 addition & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ dependencies {

androidTestCompile 'com.google.guava:guava-testlib:18.0'
androidTestCompile "com.android.support:support-v4:${SUPPORT_V4_VERSION}"
androidTestCompile "org.hamcrest:hamcrest-core:${HAMCREST_VERSION}"
androidTestCompile "org.hamcrest:hamcrest-library:${HAMCREST_VERSION}"
androidTestCompile "com.google.truth:truth:${TRUTH_VERSION}"
androidTestCompile "junit:junit:${JUNIT_VERSION}"
androidTestCompile "org.mockito:mockito-all:${MOCKITO_VERSION}"
androidTestCompile "org.robolectric:robolectric:${ROBOLECTRIC_VERSION}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

Expand Down Expand Up @@ -233,7 +232,7 @@ protected GenericRequestBuilder getRequestBuilder(Object item) {

preloader.onScroll(null, 1, 10, 30);

assertThat(loadedObjects, containsInAnyOrder(objects.toArray()));
assertThat(loadedObjects).containsAllIn(objects);
}

private static class ListPreloaderAdapter extends ListPreloader<Object> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
import java.net.URL;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
Expand Down Expand Up @@ -61,7 +59,7 @@ public void testReturnsInputStreamOnStatusOk() throws Exception {
.setResponseCode(200));
HttpUrlFetcher fetcher = getFetcher();
InputStream is = fetcher.loadData(Priority.HIGH);
assertThat(isToString(is), equalTo(expected));
assertEquals(expected, isToString(is));
}

@Test
Expand All @@ -74,7 +72,7 @@ public void testHandlesRedirect301s() throws Exception {
.setResponseCode(200)
.setBody(expected));
InputStream is = getFetcher().loadData(Priority.LOW);
assertThat(isToString(is), equalTo(expected));
assertEquals(expected, isToString(is));
}

@Test
Expand All @@ -87,7 +85,7 @@ public void testHandlesRedirect302s() throws Exception {
.setResponseCode(200)
.setBody(expected));
InputStream is = getFetcher().loadData(Priority.LOW);
assertThat(isToString(is), equalTo(expected));
assertEquals(expected, isToString(is));
}

@Test
Expand All @@ -100,11 +98,11 @@ public void testHandlesRelativeRedirects() throws Exception {
.setResponseCode(200)
.setBody(expected));
InputStream is = getFetcher().loadData(Priority.NORMAL);
assertThat(isToString(is), equalTo(expected));
assertEquals(expected, isToString(is));

mockWebServer.takeRequest();
RecordedRequest second = mockWebServer.takeRequest();
assertThat(second.getPath(), endsWith("/redirect"));
assertThat(second.getPath()).endsWith("/redirect");
}

@Test
Expand All @@ -121,11 +119,11 @@ public void testHandlesUpToFiveRedirects() throws Exception {
.setResponseCode(200).setBody(expected));

InputStream is = getFetcher().loadData(Priority.NORMAL);
assertThat(isToString(is), equalTo(expected));
assertEquals(expected, isToString(is));

assertThat(mockWebServer.takeRequest().getPath(), containsString(DEFAULT_PATH));
assertThat(mockWebServer.takeRequest().getPath()).contains(DEFAULT_PATH);
for (int i = 0; i < numRedirects; i++) {
assertThat(mockWebServer.takeRequest().getPath(), containsString(redirectBase + i));
assertThat(mockWebServer.takeRequest().getPath()).contains(redirectBase + i);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
import java.io.ByteArrayInputStream;
import java.io.InputStream;

import static org.hamcrest.Matchers.containsString;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
Expand Down Expand Up @@ -90,7 +89,7 @@ public void testDoesNotThrowIfCleanupWithNullInputStream() {
@Test
public void testContainsAllRelevantPartsInId() {
String id = harness.get().getId();
assertThat(id, containsString(harness.uri.toString()));
assertThat(id).contains(harness.uri.toString());
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,10 @@
import java.util.Map;
import java.util.concurrent.ExecutorService;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.not;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
Expand Down Expand Up @@ -90,7 +86,7 @@ public void testEngineJobReceivesRemoveCallbackFromLoadStatus() {
public void testNewRunnerIsAddedToRunnersMap() {
harness.doLoad();

assertThat(harness.jobs, hasKey((Key) harness.cacheKey));
assertThat(harness.jobs).containsKey(harness.cacheKey);
}

@Test
Expand Down Expand Up @@ -145,7 +141,7 @@ public void testKeyIsRemovedFromActiveResourcesIfRefIsCleared() {

harness.doLoad();

assertThat(harness.activeResources, not(hasKey((Key) harness.cacheKey)));
assertThat(harness.activeResources).doesNotContainKey(harness.cacheKey);
}

@Test
Expand Down Expand Up @@ -267,7 +263,7 @@ public void testRunnerIsRemovedFromRunnersOnEngineNotifiedJobComplete() {

harness.engine.onEngineJobComplete(harness.cacheKey, harness.resource);

assertThat(harness.jobs, not(hasKey((Key) harness.cacheKey)));
assertThat(harness.jobs).doesNotContainKey(harness.cacheKey);
}

@Test
Expand Down Expand Up @@ -297,7 +293,7 @@ public void testResourceIsAddedToActiveResourcesOnEngineComplete() {
@Test
public void testDoesNotPutNullResourceInActiveResourcesOnEngineComplete() {
harness.engine.onEngineJobComplete(harness.cacheKey, null);
assertThat(harness.activeResources, not(hasKey((Key) harness.cacheKey)));
assertThat(harness.activeResources).doesNotContainKey(harness.cacheKey);
}

@Test
Expand All @@ -306,7 +302,7 @@ public void testRunnerIsRemovedFromRunnersOnEngineNotifiedJobCancel() {

harness.engine.onEngineJobCancelled(harness.job, harness.cacheKey);

assertThat(harness.jobs, not(hasKey((Key) harness.cacheKey)));
assertThat(harness.jobs).doesNotContainKey(harness.cacheKey);
}


Expand Down Expand Up @@ -360,7 +356,7 @@ public void testResourceIsRemovedFromActiveResourcesWhenReleased() {

harness.engine.onResourceReleased(harness.cacheKey, harness.resource);

assertThat(harness.activeResources, not(hasKey((Key) harness.cacheKey)));
assertThat(harness.activeResources).doesNotContainKey(harness.cacheKey);
}

@Test
Expand All @@ -378,7 +374,7 @@ public void testResourceIsRecycledWhenRemovedFromCache() {
public void testJobIsPutInJobWithCacheKeyWithRelevantIds() {
harness.doLoad();

assertThat(harness.jobs, hasEntry(equalTo((Key) harness.cacheKey), equalTo(harness.job)));
assertThat(harness.jobs).containsEntry(harness.cacheKey, harness.job);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
import static android.content.ComponentCallbacks2.TRIM_MEMORY_BACKGROUND;
import static android.content.ComponentCallbacks2.TRIM_MEMORY_COMPLETE;
import static android.content.ComponentCallbacks2.TRIM_MEMORY_MODERATE;
import static org.hamcrest.Matchers.empty;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
Expand Down Expand Up @@ -51,7 +50,7 @@ public void testImmutableBitmapsAreNotAdded() {
Bitmap bitmap = createMutableBitmap();
Robolectric.shadowOf(bitmap).setMutable(false);
pool.put(bitmap);
assertThat(strategy.bitmaps, empty());
assertThat(strategy.bitmaps).isEmpty();
}

@Test
Expand Down
Loading

0 comments on commit 8588abe

Please sign in to comment.