GithubHelp home page GithubHelp logo

isabella232 / assertj-automation Goto Github PK

View Code? Open in Web Editor NEW

This project forked from palantir/assertj-automation

0.0 0.0 0.0 2.84 MB

Automatic code rewriting for AssertJ using error-prone and refaster

License: Apache License 2.0

Shell 0.02% Java 99.98%

assertj-automation's Introduction

Autorelease

assertj-automation Download

Automatic code rewriting for AssertJ using error-prone and refaster.

Why

(1) Improve failure messages in existing codebases

By making code changes like the following, we can drastically improve failure messages:

-assertTrue(continents.size() == 7);
+assertThat(continents).hasSize(7);

From this hard-to-debug message:

Expected :true
Actual   :false

To this clearer example. An engineer can now quickly diagnose what went wrong - perhaps they left a foo in recently:

java.lang.AssertionError:
Expected size:<7> but was:<8> in:
<["Africa", "Asia", "Europe", "North America", "South America", "Antarctica", "Australia", "foo"]>

There many more sub-optimal patterns that this automation can detect and fix. By codifying these patterns into tooling that can be run local and on CI, we can reduce the burden on code reviewers because all contributions will already comply with AssertJ best practises.

These fixes also help facilitate the move from JUnit 4 -> JUnit 5, as raw JUnit4 assertions can be eliminated (e.g. assertTrue and assertEquals) before actually doing the JUnit 4 -> 5 migration.

(2) Consolidate on a single assertions library

When codebases use a mixture of Hamcrest, AssertJ, raw JUnit assertions (both 4 and 5), Google Truth etc, contributors can be unsure which to use, leading to unnecessary back-and-forth during code review if they happened to pick the wrong one.

In practise, because many of these libraries are quite similar, just picking one and committing to it is a reasonable strategy. We picked AssertJ because its fluent API is has nice auto-completion properties and it's nicely extensible.

Usage: net.ltgt.errorprone

Error-prone is maintained by Google and can be used with Bazel, Maven, Gradle, Ant. Use the following Gradle:

plugins {
  // we assume you are already using the Java plugin
  id "net.ltgt.errorprone" version "0.6"
}

dependencies {
  annotationProcessor "com.palantir.assertj-automation:assertj-error-prone:<latest>" // see badge above

  errorprone "com.google.errorprone:error_prone_core:2.3.4"
  errorproneJavac "com.google.errorprone:javac:9+181-r4173-1"
}

tasks.withType(JavaCompile) {
  options.errorprone.errorproneArgs += [
    '-Xep:PreferAssertj:ERROR',
    // ... include other rules too
  ]
}

Note: refaster rules can't yet be applied from the net.ltgt.errorprone plugin, see the baseline plugin below.

Alternative usage: com.palantir.baseline

Palantir's Baseline family of plugins sets up error-prone and allows applying auto-fixes from both refaster and error-prone. Run ./gradlew compileTestJava -PerrorProneApply -PrefasterApply to apply the fixes.

plugins {
  id 'com.palantir.baseline` version '2.43.0'
}

To ensure the automatically refactored code remains readable by humans, we then run the ./gradlew formatDiff command provided by palantir-java-format. This surgically reformats the lines of code that were touched, while preserving the rest of the file.

buildscript {
  dependencies{
    classpath 'com.palantir.javaformat:gradle-palantir-java-format:0.3.9'
  }
}
apply plugin: 'com.palantir.java-format'

assertj-automation's People

Contributors

aioobe avatar alicederyn avatar ash211 avatar bavardage avatar bdworth avatar carterkozak avatar dansanduleac avatar dsd987 avatar ellisjoe avatar esword avatar ferozco avatar gatesn avatar gracew avatar iamdanfox avatar j-baker avatar jdm2212 avatar jmcampanini avatar markelliot avatar neilrickards avatar nmiyake avatar pkoenig10 avatar punya avatar qinfchen avatar raiju avatar robert3005 avatar schlosna avatar svc-autorelease avatar svc-excavator-bot avatar theberuriahincident avatar uschi2000 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.