Back to Repositories

Validating @BindInt Type Validation in ButterKnife

This test suite validates the @BindInt annotation functionality in ButterKnife, ensuring proper type checking for integer resource bindings. It focuses on verifying compile-time validation of field types when using the @BindInt annotation.

Test Coverage Overview

The test suite provides focused coverage of the @BindInt annotation’s type validation mechanism.

Key areas tested include:
  • Type validation for integer resource bindings
  • Compile-time error detection for incorrect field types
  • Error message accuracy and specificity

Implementation Analysis

The testing approach uses compile-time validation testing through the ButterKnifeProcessor. It leverages JavaFileObjects for source code generation and assertion validation, implementing a robust compile-time type checking strategy.

Key patterns include:
  • Source code string generation for test cases
  • Processor-based compilation testing
  • Error message validation

Technical Details

Testing tools and configuration:
  • JUnit test framework
  • Google Truth assertion library
  • JavaFileObjects for source generation
  • ButterKnifeProcessor for annotation processing
  • Compile-time testing utilities

Best Practices Demonstrated

The test exemplifies high-quality annotation processor testing practices. It demonstrates clear error message validation, precise line number checking, and thorough type validation.

Notable practices include:
  • Isolated test cases
  • Explicit error message verification
  • Source location validation
  • Clean test method naming

jakewharton/butterknife

butterknife-runtime/src/test/java/butterknife/BindIntTest.java

            
package butterknife;

import butterknife.compiler.ButterKnifeProcessor;
import com.google.testing.compile.JavaFileObjects;
import javax.tools.JavaFileObject;
import org.junit.Test;

import static com.google.common.truth.Truth.assertAbout;
import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource;

public final class BindIntTest {
  @Test public void typeMustBeInt() {
    JavaFileObject source = JavaFileObjects.forSourceString("test.Test", ""
        + "package test;\n"
        + "import butterknife.BindInt;\n"
        + "public class Test {\n"
        + "  @BindInt(1) String one;\n"
        + "}"
    );

    assertAbout(javaSource()).that(source)
        .processedWith(new ButterKnifeProcessor())
        .failsToCompile()
        .withErrorContaining("@BindInt field type must be 'int'. (test.Test.one)")
        .in(source).onLine(4);
  }
}