• Stars
    star
    229
  • Rank 174,666 (Top 4 %)
  • Language
    Java
  • Created over 11 years ago
  • Updated almost 11 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

BoundBox provides an easy way to test an object by accessing all its fields, constructor and methods, public or not. BoundBox breaks encapsulation.

BoundBox

BoundBox provides an easy way to test an object by accessing all its fields, constructors and methods, public or not. BoundBox breaks encapsulation.

BoundBox has been designed with Android in mind. But it will work for pure Java projects as well (J2SE and JEE), for instance to test legacy code.

To get started, have a look at BoundBox's Wiki or these slides or the samples below.

Android Sample

On Android, the class below would be very hard to test. All logic is completely imbricated into an activity life cycle method and all fields are private.

public class MainActivity extends Activity {

    // -------------------------------
    // ATTRIBUTES
    // -------------------------------

    private Button buttonMain;
    private TextView textViewMain;

    // -------------------------------
    // LIFECYCLE
    // -------------------------------

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonMain = (Button) findViewById(R.id.button_main);
        textViewMain = (TextView) findViewById(R.id.textview_main);
        buttonMain.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final int result = 42;
                textViewMain.setText(String.valueOf(result));
            }
        });
    }
}

With BoundBox, without changing anything to your activity's code, you can access its private fields, or methods.

In the test below, as soon as you write the statement : @BoundBox(boundClass=MainActivity.class), the BoundBox annotation processor will generate the class BoundBoxOfMainActivity that you can use to access all inner fields, constructors and methods of MainActivity.

An Android test becomes as easy as :

@BoundBox(boundClass = MainActivity.class, maxSuperClass = Activity.class)
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

    BoundBoxOfMainActivity boundBoxOfMainActivity;

    public MainActivityTest() {
        super(MainActivity.class);
    }

    @UiThreadTest
    public void testCompute() {
        // given
        boundBoxOfMainActivity = new BoundBoxOfMainActivity(getActivity());

        // when
        boundBoxOfMainActivity.boundBox_getButtonMain().performClick();

        // then
        assertEquals("42", boundBoxOfMainActivity.boundBox_getTextViewMain().getText());
    }

}

Note that, using FEST-Android can make things even simpler.

Pure Java Sample

Let's say we have a class A with private fields, constructors and methods like :

public class A {
 private String foo;
 
 protected A(String foo) {
   this.foo = foo;
 }
 
 private void bar(String foo) {
  this.foo += foo; 
 }
 
 protected String getFoo() {
   return "The value of foo is " + foo;
 }
}

With BoundBox, you can write a test that accesses all fields and methods of A.

Below, as soon as you write the statement : @BoundBox(boundClass=A.class), the BoundBox annotation processor will generate the class BoundBoxOfA that you can use to access all inner fields, constructors and methods of A.

public class ATest {
 @BoundBox( boundClass = A.class )
 private BoundBoxOfA boundBoxOfA;
 
 @Before
 public void setUp() {
   boundBoxOfA = new BoundBoxOfA( new A("bb") );
 }
 
 @Test
 public void testConstructor() {
   //GIVEN
   //WHEN
   //THEN
   assertEquals( "bb", boundBoxOfA.boundBox_getFoo());
 }
 
 @Test
 public void testBar() {
   //GIVEN
   //WHEN
   boundBoxOfA.bar("cc");
   
   //THEN
   assertEquals( "bbcc", boundBoxOfA.boundBox_getFoo();
 }
 
 @Test
 public void testGetFoo() {
   //GIVEN
   //WHEN
   //THEN
   assertEquals( "The value of foo is bb", boundBoxOfA.getFoo();
 }
}

Summary

BoundBox's API is quite simple. Indeed in has no API at all, just a set of conventions to access the inner structure of an Object.

BoundBox offers the following advantages over alternative technologies :

  • it doesn't pollute your API under tests. Just code clean, don't change anything for testing even not a visibility modifier.
  • objects under tests will be accessed using reflection, and this access will be checked at compile time (unlike using pure reflection or WhiteBox from PowerMock).
  • all fields, constructors and methods, even those defined in super classes are accessible. For instance, it allows to access foo.super.super.a (that is not syntactically possible in Java).

Quality of code

BoundBox is heavily tested to ensure its quality. It uses both unit and integration tests and it is placed under continuous integration. It also integrates checkstyle, findbugs, PMD to increase its robustness. Lombok is used to decrease the amount of code to write and maintain.

BoundBox is under CI on Travis

BoundBox uses coveralls.io for code coverage of unit-tests
Coverage Status

License

Copyright (C) 2013 Stéphane Nicolas

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Alternatives

More Repositories

1

robospice

Repo of the Open Source Android library : RoboSpice. RoboSpice is a modular android library that makes writing asynchronous long running tasks easy. It is specialized in network requests, supports caching and offers REST requests out-of-the box using extension modules.
Java
2,946
star
2

Quality-Tools-for-Android

Java
1,266
star
3

toothpick

A scope tree based Dependency Injection (DI) library for Java / Kotlin / Android.
Java
1,123
star
4

RoboDemo

RoboDemo is a ShowCase library for Android to demonstrate to users how a given Activity works.
Java
215
star
5

loglifecycle

Logs all lifecycle methods of annotated Activities, Fragments, Services, Views, etc.
Java
206
star
6

reflection-no-reflection

A proof on concept to create an API that is 100% compatible reflection API, but without any reflection. Performance stuff for Android and Java.
Java
130
star
7

ormlite-android-gradle-plugin

A Gradle plugin for Android to generate an ORMLite configuration file and boost DAOs creations.
Java
102
star
8

morpheus

An Android gradle plugin to transform classes via javassist.
Java
80
star
9

afterburner

A library to help other librairies getting rid of boiler plate via byte code manipulation. Works on Android too.
Java
74
star
10

injectextra

Injects everything passed by extra to an activity automatically.
Java
26
star
11

androidstudio4maccheatsheet

Most useful keyboard shortcuts with default keymap in Android Studio.
23
star
12

mimic

Mimicing is, indeed, kind of way to bypass java single inheritance paradigm. It allows to copy all declared fields, constructors and methods in a given class into another class.
Java
19
star
13

activtity-fragment-lambda

Example of how to use lambdas to communicate between Fragments and Activities
Java
17
star
14

injectview

On Android, Injects all views inflated from XML automatically. No boiler plate at all.
Java
17
star
15

injects

This repo is a control center for the injects projects.
9
star
16

injectresource

Inject resources automatically on Android.
Java
8
star
17

javassist-build-plugin-api

A unified API to create class transformers based on javassist during maven and gradle builds.
Java
7
star
18

TypedAdapter

Object Oriented android list view adapter(s) using generics to display given Pojos.
Java
5
star
19

google-guice

A fork of Google Guice to allow RoboGuice to go faster at startup.
Java
3
star
20

zest-android

Sample app to demonstrate roboguice usage + testing
Java
3
star
21

Dependency-Resources-Linearization

Demonstrate that the linearization of the dependency graph that gives to the order of priority to aapt2 when merging resources is hard to grasp for humans and that we can quickly have problems when scaling the number of modules.
Kotlin
3
star
22

MixedServiceLifeCycle-sample

A sample to demonstrate that a mixed life cycle service (both started and bound) is buggy. Built for a common guy ;)
Java
2
star
23

heatControl

Controls a custom micro controller on a heating system
Kotlin
1
star
24

bugs-instant-execution

Sample project to illustrate instant execution bugs in Android projects
Shell
1
star