Back to Repositories

Validating Float Resource Binding Types in ButterKnife

This test suite validates the @BindFloat annotation functionality in ButterKnife, ensuring proper type validation for float bindings. It focuses on verifying type safety and compile-time checks for float resource bindings.

Test Coverage Overview

The test suite covers type validation for the @BindFloat annotation, specifically ensuring that fields annotated with @BindFloat must be of type float. It verifies compile-time type checking and error handling.

  • Tests field type validation for float bindings
  • Verifies compile-time error generation
  • Validates error message content and location

Implementation Analysis

The testing approach uses compile-time verification through the JavaFileObjects utility to create test source files. It leverages the ButterKnifeProcessor to process annotations and verify compilation failures.

The implementation uses Google’s Truth assertion framework and compile-testing utilities to validate compiler behavior and error messages.

Technical Details

  • JUnit test framework for test organization
  • Google Truth for assertions
  • compile-testing library for compilation validation
  • ButterKnifeProcessor for annotation processing
  • JavaFileObjects for test source creation

Best Practices Demonstrated

The test demonstrates excellent practices in annotation processor testing, including precise error message validation and line number checking. It shows proper use of compile-time testing patterns and clear test case organization.

  • Specific error message validation
  • Source location verification
  • Clean test case structure
  • Effective use of testing utilities

jakewharton/butterknife

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

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