Feeds:
Posts
Comments

Archive for the ‘Java’ Category

This is the latest update for Java SE 6.

Download
Release Notes

Read Full Post »

This is the latest update for Java SE 6.

Download
Release Notes

Read Full Post »

Introduction

Unit Testing verifies that the smallest bits of testable source code behave exactly as expected. Test-driven development is based on Unit Testing, and the use of automated testing frameworks.
The most popular Java testing framework is JUnit. Unfortunately JUnit cannot work in J2ME projects, since J2ME does not support Reflection. However J2MEUnit comes to the rescue, as it is based on the original JUnit source code, and it is designed to work with J2ME.
This article explains how to integrate J2MEUnit with Eclipse, and write a simple Unit test.

Setting up the environment

I am assuming that Java, WTK, Eclipse and MTJ are already installed and properly configured.
For J2MEUnit, simply download the j2meunit.jar file.

Creating a new MIDlet project

Create a new MIDlet project on Eclipse.

unit_testing_j2me_eclipse_1

Since this is a “hello world” type application, give it a simple name like “MobileUnitTest”.

Use the default properties on the Target Device:

unit_testing_j2me_eclipse_2

Creating a “Hello World” Midlet

Right click on the “src” folder and click on New -> Java ME MIDlet
Name it “HelloUnitWorld”, and use the “mobile.unit” as package name.

You should now see the empty auto-generated methods on the newly created MIDlet.

Let’s add some minimal code, in order to run our “Hello World” application.

1. Add a new private variable on the class with the following code:

private Display display;

2. Add the following code on the “startApp()” method:

display = Display.getDisplay(this);
String message = "Hello Unit World!";
TextBox textBox = new TextBox("HelloUnitWorld", message , 50, 0);
display.setCurrent(textBox);

Run the application and you should get something like that:

unit_testing_j2me_eclipse_3

Adding some testable code

We need to add some testable code for this tutorial.
So let’s create something simple, a new Java class, named “Calculator”, having the following code:

package mobile.unit;

public class Calculator {

	public int Add(int firstValue, int secondValue){
		return firstValue + secondValue;
	}

}

The method “Add” accepts two integer parameters, adds them, and returns the sum as integer.

Now we are going to modify the “startApp()” MIDlet method, to output a sum result.

//String message = "Hello Unit World!";
String message = String.valueOf(new Calculator().Add(3, 4));

If we run the application again, it should display the sum of 3+4:

unit_testing_j2me_eclipse_4

Adding the test code

Here comes the interesting part, adding the test code.

1. We need to reference the j2meunit.jar, which we have downloaded before.
Project -> Properties -> Java Build Path -> Add External JARs… -> Select j2meunit.jar

2. Create a new source folder named “tests”.

3. Create a new Java class on the “tests” folder with the name “CalculatorTests”, on a new package “mobile.unit.tests”.

Every test class has to import the j2meunit.framework, and extend the “TestCase”.
Moreover it should have two default new constructors.
Since there is no Reflection support, we need to manually add every test method on a “TestSuite”, and have a method that returns the “TestSuite” instance.
The test methods do not have to start with the “test” prefix, however it’s a good practice to have it.
If that sounds a bit confusing, please have a look on the complete CalculatorTests.java code:

package mobile.unit.tests;

import j2meunit.framework.*;
import mobile.unit.Calculator;

public class CalculatorTests extends TestCase {

	public CalculatorTests(){
		super();
	}

	public CalculatorTests(String name, TestMethod method){
		super(name, method);
	}

	public Test suite(){
		TestSuite suite = new TestSuite();
		suite.addTest(new CalculatorTests("testAdd", new TestMethod()
				{public void run(TestCase tc) {((CalculatorTests) tc).testAdd();}}));
		return suite;
	}

	public void testAdd(){
		assertEquals(10, new Calculator().Add(5, 6));
	}

}

The actual test code is contained on the “testAdd()” method.
We are trying to add 5+6, and expect a sum of 10, which is clearly going to fail.

Running the tests

Here comes the tricky part, we are going to use the j2meunit console TestRunner.
Go to Run -> Run Configurations -> Java Application -> New Configuration
Project: MobileUnitTest
Main class: j2meunit.textui.TestRunner
Arguments: mobile.unit.tests.CalculatorTests

If you fail to include the correct package on the Arguments, you are going to get an ugly error on the console:

TestRunner.main()
Could not create and run test suite

If everything goes fine, the console will output:

FAILURES!!!
Test Results:
Run: 1 Failures: 1 Errors: 0
There was 1 failure:
1) testAdd(mobile.unit.tests.CalculatorTests) “expected:<10> but was:<11>”

This is our expected test failure!

Let’s modify the “testAdd()” method to properly pass the test:

assertEquals(11, new Calculator().Add(5, 6));

Run it again, and get on the console:

OK (1 tests)

Conclusion

As I have demonstrated on this tutorial, J2MEUnit requires some effort to set up the tests, and add all required code.
However the result is really pleasant, as it gives you a fully working J2ME testing environment.
And that’s what matters most at the end day, because “Debugging sucks, Testing rocks!”.

Read Full Post »

This is the latest update for Java SE 6.

Download
Release Notes

Read Full Post »

NetBeans 6.5

Release Date: Nov 19, 2008

NetBeans IDE 6.5 offers simplified and rapid development of web, enterprise, desktop, and mobile applications with PHP, JavaScript, Java, C/C++, Ruby, and Groovy.

New features include a robust IDE for PHP, JavaScript debugging for Firefox and IE, and support for Groovy and Grails. The release also delivers a number of enhancements for Java, Ruby on Rails, and C/C++ development. Java highlights include: built-in support for Hibernate, Eclipse project import, and compile on save. Combining excellent out of the box experience, compelling features, and a great plugin ecosystem, NetBeans IDE 6.5 is a must-download for all developers.

Download

Read Full Post »

This is the latest update for Java SE 6.

Download
Release Notes

Read Full Post »

This is the seventh update for Java SE 6.

Java VisualVM is a new GUI-based tool for troubleshooting Java applications and is available as part of Java SE 6 Update 7. VisualVM incorporates technologies such as jvmstat, JMX, the NetBeans profiler, and more

Download
Release Notes

Read Full Post »

NetBeans 6.1 – Final

Here is the latest NetBeans version:

Download

Read Full Post »

This is the sixth update for Java SE 6.

Download
Release Notes

Read Full Post »

This is the fifth update for Java SE 6.

Download
Release Notes

Read Full Post »

« Newer Posts - Older Posts »