lint-junit-rule

Additional

Language
Java
Version
v0.3.0 (Oct 7, 2015)
Created
Apr 19, 2015
Updated
Aug 24, 2017 (Retired)
Owner
André Diermann (a11n)
Contributor
André Diermann (a11n)
1
Activity
Badge
Generate
Download
Source code

Advertisement

lint-junit-rule

A JUnit rule which allows unit testing of custom Lint rules.

If you want to learn more about custom Lint rules please check this reference guide. If you want to write your own custom Lint rule you could use this custom Lint rule template. If you are still looking for more details you are welcome to check this workshop on custom Lint rules.

Motivation

Fortunately, with the release of the lint-tests library version 24.3.0 at the end of July 2015 [1] there is now official test support available for custom Lint rules. Unfortunately there are still some drawbacks:

  1. You have to use the JUnit 3 approach of extending LintDetectorTest.
  2. The assertion options are poor, all you get is one String.
  3. There is an issue when loading your test resources [2].

Therefore the objective of this library is to take you one step further:

  1. It provides JUnit 4 support by wrapping the official test support into a dedicated JUnit rule.
  2. Furthermore it exposes the internal Lint Warnings for you which allows powerful assertions.
  3. Last but not least it fixes the aforementioned issue for you which gives you a convenient way to place your test resources in test/res.

Usage

Import the dependency in your build.gradle. It's available via JCenter:

dependencies{
  ...
  testCompile 'de.ad:lint-junit-rule:0.3.0'
  
  //optional
  testCompile 'de.ad:lint-assertj:0.3.0'
  testCompile 'de.ad:lint-hamcrest:0.3.0'
}

Apply the rule in your test class and specify the Detector as well as the Issues under test. Next provide the files to analyze. Finally do your assertions on the populated Warnings:

  @Rule public Lint lint = new Lint(new MyCustomRule(), MyCustomRule.ISSUE);

  @Test
  public void test() throws Exception {
    List<Warning> lintResult = lint.files("AndroidManifest.xml", "res/values/strings.xml");

    //AssertJ
    assertThat(lintResult)
      .hasWarnings(2)
      .in("AndroidManifest.xml", "strings.xml")
      .atLine(8, 14)
      .withMessage("MyCustomRule warning message.",
                   "MyCustomRule warning message.");

    //Hamcrest
    assertThat(lintResult,
                hasWarnings(
                  in("AndroidManifest.xml", "strings.xml"),
                  atLine(8, 14),
                  withMessage("MyCustomRule warning message.",
                              "MyCustomRule warning message.")));
  }

Note: Test resources are looked-up in test/res.

References

  1. https://bintray.com/android/android-tools/com.android.tools.lint.lint-tests/24.3.0/view
  2. https://code.google.com/p/android/issues/detail?id=182195