Enforce use of assertThat

Assert.assert* methods other than assertThat() produce less clear tests.
This change enforces using assertThat() for all verifications.
While more verbose they are generally more consistent and easier to understand.
This commit is contained in:
Liam Newman
2021-04-20 00:46:43 -07:00
parent 3d451526ef
commit d13e490be2
3 changed files with 23 additions and 75 deletions

View File

@@ -271,76 +271,6 @@ public abstract class AbstractGitHubWireMockTest {
MatcherAssert.assertThat(reason, assertion);
}
@Deprecated
public static void assertEquals(Object expected, Object actual) {
fail("In this project we use 'assertThat(actual, equalTo(expected))");
}
@Deprecated
public static void assertEquals(String reason, Object expected, Object actual) {
fail("In this project we use 'assertThat(reason, actual, equalTo(expected))");
}
@Deprecated
public static void assertNotEquals(Object expected, Object actual) {
fail("In this project we use 'assertThat(actual, not(expected))");
}
@Deprecated
public static void assertNotEquals(String reason, Object expected, Object actual) {
fail("In this project we use 'assertThat(reason, actual, not(expected))");
}
@Deprecated
public static void assertNotNull(Object actual) {
fail("In this project we use 'assertThat(actual, notNullValue())");
}
@Deprecated
public static void assertNotNull(String reason, Object actual) {
fail("In this project we use 'assertThat(reason, actual, notNullValue())");
}
@Deprecated
public static void assertNull(Object actual) {
fail("In this project we use 'assertThat(actual, nullValue())");
}
@Deprecated
public static void assertNull(String reason, Object actual) {
fail("In this project we use 'assertThat(reason, actual, nullValue())");
}
@Deprecated
public static void assertSame(Object expected, Object actual) {
fail("In this project we use 'assertThat(actual, sameInstance(expected))");
}
@Deprecated
public static void assertSame(String reason, Object expected, Object actual) {
fail("In this project we use 'assertThat(reason, actual, sameInstance(expected))");
}
@Deprecated
public static void assertTrue(Boolean condition) {
fail("In this project we use 'assertThat(condition, is(true)) or more appropirate matchers");
}
@Deprecated
public static void assertTrue(String reason, Boolean condition) {
fail("In this project we use 'assertThat(reason, condition, is(true)) or more appropirate matchers");
}
@Deprecated
public static void assertFalse(Boolean condition) {
fail("In this project we use 'assertThat(condition, is(true)) or more appropirate matchers");
}
@Deprecated
public static void assertFalse(String reason, Boolean condition) {
fail("In this project we use 'assertThat(reason, condition, is(false)) or more appropirate matchers");
}
protected static class TemplatingHelper {
public Date testStartDate = new Date();

View File

@@ -1209,15 +1209,15 @@ public class AppTest extends AbstractGitHubWireMockTest {
GHRepository r = gitHub.getRepository("hub4j/github-api");
String sha1 = "a12243f2fc5b8c2ba47dd677d0b0c7583539584d";
assertBlobContent(r.readBlob(sha1));
verifyBlobContent(r.readBlob(sha1));
GHBlob blob = r.getBlob(sha1);
assertBlobContent(blob.read());
verifyBlobContent(blob.read());
assertThat(blob.getSha(), is("a12243f2fc5b8c2ba47dd677d0b0c7583539584d"));
assertThat(blob.getSize(), is(1104L));
}
private void assertBlobContent(InputStream is) throws Exception {
private void verifyBlobContent(InputStream is) throws Exception {
String content = new String(IOUtils.toByteArray(is), StandardCharsets.UTF_8);
assertThat(content, containsString("Copyright (c) 2011- Kohsuke Kawaguchi and other contributors"));
assertThat(content, containsString("FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR"));

View File

@@ -9,8 +9,7 @@ import com.tngtech.archunit.lang.ArchRule;
import org.junit.BeforeClass;
import org.junit.Test;
import static com.tngtech.archunit.lang.conditions.ArchConditions.beAnnotatedWith;
import static com.tngtech.archunit.lang.conditions.ArchConditions.not;
import static com.tngtech.archunit.lang.conditions.ArchConditions.*;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.fields;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods;
@@ -23,6 +22,11 @@ public class ArchTests {
.withImportOption(new ImportOption.DoNotIncludeJars())
.importPackages("org.kohsuke.github");
private static final JavaClasses tesetClassFiles = new ClassFileImporter()
.withImportOption(new ImportOption.OnlyIncludeTests())
.withImportOption(new ImportOption.DoNotIncludeJars())
.importPackages("org.kohsuke.github");
private static final DescribedPredicate<JavaAnnotation<?>> previewAnnotationWithNoMediaType = new DescribedPredicate<JavaAnnotation<?>>(
"preview has no required media types defined") {
@@ -105,4 +109,18 @@ public class ArchTests {
methodRule.check(classFiles);
}
@Test
public void testRequireUseOfAssertThat() {
String reason = "This project uses `assertThat(...)` instead of other assert*() methods.";
ArchRule onlyAssertThatRule = methods().that()
.haveNameContaining("assert")
.should()
.haveName("assertThat")
.because(reason);
onlyAssertThatRule.check(tesetClassFiles);
}
}