Back to Repositories

Validating Bitmap Annotation Type Safety in Butterknife

This test suite validates the @BindBitmap annotation functionality in the Butterknife library, specifically focusing on type validation for bitmap bindings. The tests ensure proper compile-time validation of bitmap field types and error handling.

Test Coverage Overview

The test coverage focuses on compile-time validation of the @BindBitmap annotation, ensuring type safety for bitmap bindings.

Key areas covered include:
  • Type validation for bitmap fields
  • Compile-time error detection
  • Error message accuracy verification

Implementation Analysis

The testing approach utilizes Google’s compile-testing framework to validate annotation processing at compile time. The implementation leverages JavaFileObjects for source code generation and assertion validation using Truth assertions framework.

Testing patterns include:
  • Compile-time annotation processing
  • Error message validation
  • Source file manipulation

Technical Details

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

Best Practices Demonstrated

The test suite demonstrates several testing best practices for annotation processing validation.

Notable practices include:
  • Isolated test cases for specific validation scenarios
  • Clear error message verification
  • Proper test setup and resource management
  • Effective use of compilation testing utilities

jakewharton/butterknife

butterknife-runtime/src/test/java/butterknife/BindBitmapTest.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 BindBitmapTest {
  @Test public void typeMustBeBitmap() {
    JavaFileObject source = JavaFileObjects.forSourceString("test.Test", ""
        + "package test;\n"
        + "import butterknife.BindBitmap;\n"
        + "public class Test {\n"
        + "  @BindBitmap(1) String one;\n"
        + "}"
    );

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