Skip to content
This repository has been archived by the owner on Jun 18, 2020. It is now read-only.

Commit

Permalink
Add checkstyle rules for JUnit assert methods. (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
serj-lotutovici committed Oct 17, 2016
1 parent 26d57b6 commit 74d5560
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 123 deletions.
9 changes: 4 additions & 5 deletions api-client/src/test/java/com/xing/api/CallSpecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.junit.Assert.assertNotNull;

@SuppressWarnings({"MagicNumber", "ConstantConditions"})
public class CallSpecTest {
Expand Down Expand Up @@ -815,7 +814,7 @@ public void specRawStreamsSuccessResponse() throws Exception {
BlockingObservable<Response<TestMsg, Object>> blocking = spec.rawStream().toBlocking();
Response<TestMsg, Object> response = blocking.first();

assertNotNull(response.body());
assertThat(response.body()).isNotNull();
assertThat(response.body().code).isEqualTo(200);
assertThat(response.body().msg).isEqualTo("success");
}
Expand All @@ -841,7 +840,7 @@ public void specRawStreamsErrorResponse() throws Exception {
Response<Object, HttpError> response = blocking.first();

assertThat(response.isSuccessful()).isFalse();
assertNotNull(response.error());
assertThat(response.error()).isNotNull();
assertThat(response.error().message()).isEqualTo("Terrible Error.");
assertThat(response.error().name()).isEqualTo("TEST_ERROR");
assertThat(response.error().errors().get(0))
Expand Down Expand Up @@ -1172,7 +1171,7 @@ private static void assertSuccessResponse(Response<TestMsg, Object> response, Te
assertThat(response.headers()).isNotNull();

TestMsg msg = response.body();
assertNotNull(msg);
assertThat(msg).isNotNull();
assertThat(msg.msg).isEqualTo(expected.msg);
assertThat(msg.code).isEqualTo(expected.code);
}
Expand All @@ -1183,7 +1182,7 @@ private static void assertErrorResponse(Response<Object, TestMsg> response, Test
assertThat(response.body()).isNull();

TestMsg body = response.error();
assertNotNull(body);
assertThat(body).isNotNull();
assertThat(body.msg).isEqualTo(expected.msg);
assertThat(body.code).isEqualTo(expected.code);
}
Expand Down
9 changes: 4 additions & 5 deletions api-client/src/test/java/com/xing/api/ConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import okio.Buffer;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

/**
Expand Down Expand Up @@ -266,14 +265,14 @@ public void firstInList() throws Exception {
@Test
public void firstAsNullIfListEmpty() throws Exception {
Type compositeType1 = Converter.first(TestData.class, "empty");
assertNull(fromJson(compositeType1, "{\n"
assertThat(fromJson(compositeType1, "{\n"
+ " \"empty\": null\n"
+ '}'));
+ '}')).isNull();

Type compositeType2 = Converter.first(TestData.class, "empty");
assertNull(fromJson(compositeType2, "{\n"
assertThat(fromJson(compositeType2, "{\n"
+ " \"empty\": []\n"
+ '}'));
+ '}')).isNull();
}

@Test
Expand Down
28 changes: 14 additions & 14 deletions api-client/src/test/java/com/xing/api/UrlEscapeUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;

/**
Expand Down Expand Up @@ -66,10 +64,10 @@ public void actsAsUrlFormParameterEscaper() {
assertUnicodeEscaping("%F0%90%80%80", '\uD800', '\uDC00');
assertUnicodeEscaping("%F4%8F%BF%BF", '\uDBFF', '\uDFFF');

assertEquals("", UrlEscapeUtils.escape(""));
assertEquals("safestring", UrlEscapeUtils.escape("safestring"));
assertEquals("embedded%00null", UrlEscapeUtils.escape("embedded\0null"));
assertEquals("max%EF%BF%BFchar", UrlEscapeUtils.escape("max\uffffchar"));
assertThat(UrlEscapeUtils.escape("")).isEqualTo("");
assertThat(UrlEscapeUtils.escape("safestring")).isEqualTo("safestring");
assertThat(UrlEscapeUtils.escape("embedded\0null")).isEqualTo("embedded%00null");
assertThat(UrlEscapeUtils.escape("max\uffffchar")).isEqualTo("max%EF%BF%BFchar");

// Specified as safe by RFC 2396 but not by java.net.URLEncoder.
assertEscaping("%21", '!');
Expand All @@ -82,8 +80,8 @@ public void actsAsUrlFormParameterEscaper() {
assertEscaping("%20", ' ');
assertEscaping("%2B", '+');

assertEquals("safe%20with%20spaces", UrlEscapeUtils.escape("safe with spaces"));
assertEquals("foo%40bar.com", UrlEscapeUtils.escape("foo@bar.com"));
assertThat(UrlEscapeUtils.escape("safe with spaces")).isEqualTo("safe%20with%20spaces");
assertThat(UrlEscapeUtils.escape("foo@bar.com")).isEqualTo("foo%40bar.com");
}

/**
Expand All @@ -94,8 +92,9 @@ public void actsAsUrlFormParameterEscaper() {
*/
private static void assertEscaping(String expected, char c) {
String escaped = computeReplacement(c);
assertNotNull(escaped);
assertEquals(expected, escaped);
assertThat(escaped)
.isNotNull()
.isEqualTo(expected);
}

/**
Expand All @@ -104,7 +103,7 @@ private static void assertEscaping(String expected, char c) {
* @param c the character to test
*/
private static void assertUnescaped(char c) {
assertNull(computeReplacement(c));
assertThat(computeReplacement(c)).isNull();
}

/**
Expand All @@ -118,8 +117,9 @@ private static void assertUnescaped(char c) {
private static void assertUnicodeEscaping(String expected, char hi, char lo) {
int cp = Character.toCodePoint(hi, lo);
String escaped = computeReplacement(cp);
assertNotNull(escaped);
assertEquals(expected, escaped);
assertThat(escaped)
.isNotNull()
.isEqualTo(expected);
}

private static String computeReplacement(char c) {
Expand Down
5 changes: 2 additions & 3 deletions api-client/src/test/java/com/xing/api/XingApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
Expand Down Expand Up @@ -219,7 +218,7 @@ public void onFailure(Throwable t) {
t.printStackTrace();
}
});
assertTrue(latch.await(2, TimeUnit.SECONDS));
assertThat(latch.await(2, TimeUnit.SECONDS)).isTrue();

verify(executor).execute(any(Runnable.class));
verifyNoMoreInteractions(executor);
Expand Down Expand Up @@ -255,7 +254,7 @@ public void onFailure(Throwable t) {
latch.countDown();
}
});
assertTrue(latch.await(2, TimeUnit.SECONDS));
assertThat(latch.await(2, TimeUnit.SECONDS)).isTrue();

verify(executor).execute(any(Runnable.class));
verifyNoMoreInteractions(executor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

import static org.apache.commons.lang3.StringUtils.leftPad;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/** Only test methods that represent some functionality. */
Expand Down Expand Up @@ -82,18 +80,18 @@ public void setDescriptionThrowsIfLimitExited() throws Exception {
@Test
public void companyReadyToBeAdded() throws Exception {
Company company = new Company();
assertFalse(company.isFilledForAddCompany());
assertThat(company.isFilledForAddCompany()).isFalse();

company.name("Test Name");
assertFalse(company.isFilledForAddCompany());
assertThat(company.isFilledForAddCompany()).isFalse();

company.title("Test Title");
assertFalse(company.isFilledForAddCompany());
assertThat(company.isFilledForAddCompany()).isFalse();

company.industries(Collections.singletonList(new Industry(22202, "TEST_IND")));
assertFalse(company.isFilledForAddCompany());
assertThat(company.isFilledForAddCompany()).isFalse();

company.formOfEmployment(FormOfEmployment.PARTNER);
assertTrue(company.isFilledForAddCompany());
assertThat(company.isFilledForAddCompany()).isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,16 @@
import java.util.Collections;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

@SuppressWarnings("ConstantConditions")
public final class GeoCodeJsonAdapterTest {
private final Moshi moshi = new Moshi.Builder().build();

@Test
public void ignoresOtherTypes() throws Exception {
assertNull(geoCodeAdapter(String.class));
assertNull(geoCodeAdapter(Double.class));
assertNotNull(geoCodeAdapter(GeoCode.class));
assertThat(geoCodeAdapter(String.class)).isNull();
assertThat(geoCodeAdapter(Double.class)).isNull();
assertThat(geoCodeAdapter(GeoCode.class)).isNotNull();
}

@Test
Expand All @@ -61,11 +59,11 @@ public void validGeoCode() throws Exception {
public void ignoresInvalidGeoCode() throws Exception {
JsonAdapter<GeoCode> adapter = geoCodeAdapter(GeoCode.class);

assertNull(adapter.fromJson("{\"accuracy\":12,\"latitude\":null,\"longitude\":null}"));
assertNull(adapter.fromJson("{\"latitude\":34.2,\"longitude\":null}"));
assertNull(adapter.fromJson("{\"latitude\":null,\"longitude\":24.45}"));
assertNull(adapter.fromJson("{\"unknown\":null,\"longitude\":24.45}"));
assertNull(adapter.fromJson("{}"));
assertThat(adapter.fromJson("{\"accuracy\":12,\"latitude\":null,\"longitude\":null}")).isNull();
assertThat(adapter.fromJson("{\"latitude\":34.2,\"longitude\":null}")).isNull();
assertThat(adapter.fromJson("{\"latitude\":null,\"longitude\":24.45}")).isNull();
assertThat(adapter.fromJson("{\"unknown\":null,\"longitude\":24.45}")).isNull();
assertThat(adapter.fromJson("{}")).isNull();
}

@SuppressWarnings("unchecked") // It's the callers responsibility.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;

/**
Expand All @@ -41,13 +40,13 @@ public class PhoneJsonAdapterTest {
public void ignoresOtherTypes() throws Exception {
JsonAdapter<?> adapter1 = PhoneJsonAdapter.FACTORY.create(
String.class, Collections.<Annotation>emptySet(), moshi);
assertNull(adapter1);
assertThat(adapter1).isNull();

Set<Annotation> annotations = new LinkedHashSet<>(1);
annotations.add(mock(Annotation.class));
JsonAdapter<?> adapter2 = PhoneJsonAdapter.FACTORY.create(
Phone.class, annotations, moshi);
assertNull(adapter2);
assertThat(adapter2).isNull();
}

@Test
Expand Down
Loading

0 comments on commit 74d5560

Please sign in to comment.