Back to Repositories

Validating Multiple View Binding Implementation in Butterknife

This integration test suite validates the @BindViews functionality in Butterknife, focusing on binding multiple views to both arrays and lists. The tests verify proper view binding, ordering, and cleanup through unbinding operations.

Test Coverage Overview

The test suite provides comprehensive coverage of @BindViews annotation functionality, testing both array and list implementations.

Key areas tested include:
  • View binding to arrays
  • View binding to List collections
  • Correct order preservation of bound views
  • Proper cleanup during unbinding

Implementation Analysis

The testing approach utilizes JUnit for structured test cases and Google Truth for assertions. The implementation creates a synthetic view hierarchy using ViewTree and validates both array and List bindings through parallel test methods.

Key patterns include:
  • Consistent setup-execute-verify structure
  • Isolated test cases for different collection types
  • Explicit verification of view ordering
  • Cleanup validation through unbinding checks

Technical Details

Testing tools and configuration:
  • JUnit test framework
  • Google Truth assertion library
  • Butterknife annotation processor
  • Custom ViewTree test utility
  • Android view hierarchy simulation

Best Practices Demonstrated

The test suite exemplifies several testing best practices for Android view binding.

Notable practices include:
  • Separate test cases for different collection types
  • Explicit cleanup verification
  • Clear test method naming
  • Consistent assertion patterns
  • Resource cleanup through unbinding

jakewharton/butterknife

butterknife-integration-test/src/androidTest/java/com/example/butterknife/functional/BindViewsTest.java

            
package com.example.butterknife.functional;

import android.view.View;
import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import java.util.List;
import org.junit.Test;

import static com.google.common.truth.Truth.assertThat;

public final class BindViewsTest {
  static class TargetViewArray {
    @BindViews({1, 2, 3}) View[] actual;
  }

  @Test public void array() {
    View tree = ViewTree.create(1, 2, 3);
    View expected1 = tree.findViewById(1);
    View expected2 = tree.findViewById(2);
    View expected3 = tree.findViewById(3);

    TargetViewArray target = new TargetViewArray();
    Unbinder unbinder = ButterKnife.bind(target, tree);
    assertThat(target.actual).asList().containsExactly(expected1, expected2, expected3).inOrder();

    unbinder.unbind();
    assertThat(target.actual).isNull();
  }
  static class TargetViewList {
    @BindViews({1, 2, 3}) List<View> actual;
  }

  @Test public void list() {
    View tree = ViewTree.create(1, 2, 3);
    View expected1 = tree.findViewById(1);
    View expected2 = tree.findViewById(2);
    View expected3 = tree.findViewById(3);

    TargetViewList target = new TargetViewList();
    Unbinder unbinder = ButterKnife.bind(target, tree);
    assertThat(target.actual).containsExactly(expected1, expected2, expected3).inOrder();

    unbinder.unbind();
    assertThat(target.actual).isNull();
  }
}