Introduction – Eclipse, Android and JUnits
In my last article, An introduction to Junits in Android development environment, we had a first contact with JUnits and testing development in a general way, with no examples using Android. Now, let´s learn how to join the JUnits and the Android development environment we are working on, in this case, using Eclipse.
We have different ways of starting this:
-We can create the testing classes in the same Android project we are working on.
-We can create the testing classes in one different project.
The choice is up to you. How will we do it? We are going to use a different project for testing classes. This way, we will have our main code clean, separated from the testing classes.
Let's create the projects: in the image you can see both projects. One is called BrighHubExample and other one is called BrighHubExampleTest.
At first sight they both seem similar but they are not: In the main project (BrightHubExample) we have created an Activity (MainHub.java) and there is nothing created in the Testing project but the Android project structure.
Let's check and compare the Android Manifest xml files in both projects:
In the Main project we have the following
The usual Android manifest file, with the MainHub class declared as Activity.
Now let's check the Testing Manifest:
Here we have something new. Where are the Activities declarations? What do these lines mean?
In a Android Testing Project, we don't need anything more than this:
<uses-library android:name="android.test.runner" />
<instrumentation android:targetPackage="com.hub.example" android:name="android.test.InstrumentationTestRunner" />
With these lines, we make this project to use testing libraries. Here is an important point:
Here we show that the target package for our testing is the com.hub.example package, the main project package.
We need one last thing to make the testing project work as desired; We need to add the Junit library to the project path.
Right click over the project → Properties → Java Build Path → Libraries
Here we click on the button “Add Library". We select Junit from the list and make sure we are clicking the “Junit4" library. Once this is done, we have the Junit4 library in our project, ready to be used.
So, we are ready to start working with tests!