Java

How to use CodePro AnalytiX to develop Automated Unit testing

Sometimes, we need to develop Automated Unit testing for a complicated logic or some utility functions that we can not execute directly from our application. For example, we and client are developing a interesting module at the same time, we need to create some stubs or drivers or mock objects for our logic can be executed. How do we do that?

There are many ways to do that, but mainly are: manual testing and automated testing.

Manual testing:

Write stubs or drivers that we need
Write a “main” function to test for our logic.
Execute environment set-up code…

Automated testing:

Write @Test function
Do not need stubs or drivers
Use mocking technique to create mock objects. Very simple and quickly

Advantaged of automated testing over manual testing:

Save time.
Professional.

How about the disadvantaged:

We need to learn how to write automated testing

OK. Let’s get started with CodePro AnalytiX – an Automated testing tool by setting up that tool in Eclipse, and playing around with some “real-world” code:

Install

Required: Eclipse 3.4+
Go to this site and find the update link corresponding to your Eclipse version.

Here I use Eclipse 3.7 (Indigo)
http://dl.google.com/eclipse/inst/codepro/latest/3.7

Note: Google was not published any new version since 3.7. So if you are using an Eclipse version that is greater than 3.7, please use …

Go to Eclipse menu Help –> Install New Softwares…

1

Enter the CodePro AnalytiX update link into “Work with:” text box. Select CodePro item and install it by clicking Next, accept the term of use, then Finish. You might need to restart the Eclipse after installation to to take effect.

Using CodePro AnalytiX

It’s very easy to start using CodePro AnalytiX.

Assume we have a project called “MyBusiness“.

A class with name “Range“. Does it make sense with Project objective. NO!!!

package com.mycomp.product;
    public class Range {
    private double begin;
 
    private double end;
 
    Range(double begin, double end) {
        if(end < begin) {             throw new IllegalArgumentException("inLength must be larger or equals 0");         }         this.begin = begin;         this.end = end;     }     boolean isIntersect(Range inOtherRange) {         return end > inOtherRange.begin && begin < inOtherRange.end;
    }
 
    boolean isInclude(Range inOtherRange) {
        return begin <= inOtherRange.begin && end >= inOtherRange.end;
    }
 
    @Override
    public String toString() {
    return String.format("[%f, %f]", begin, end);
    }
}

Now, right-click on class that you want to generate the test case as well as test class and test project if it not exist, then select: CodePro Tools –> Generate Test Cases

CodePro Tools will do his job right away. You can see a new project “MyBusinessTest” is generated.

Structure of Test project:

TestProjectStructure

There is class RangeTest. It is test class for Range.

RangeTest is also put in the package com.mycomp.product. That means, test code is able to access to the production code with package level accessing. It’s very nice and also is the best practice for Test project structure.

For each level of package, we have a TestAll class. If we want to run all test classes inside the package com.mycomp.product, run the class com.mycomp.product.TestAll. Run class com.mycomp.TestAll to execute whole test classes in project.

Look inside the test class, CodePro AnalytiX has generated some test method. But it seems not benefit for us, so just simply delete it except main method.setUp and tearDown are also need in case you need some configuration before and after executing any test case.

OK, now let add some test case from scratch, by typing:

@Test
public void testName() throws Exception {
}

Go ahead and write some amazing test.

Run the test:

Right-click on test class or TestAll class and choose: Run As –> JUnit Test (Shortcut key are the best choice when running test)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s