Working and Testing with Ant

Recently I've been working with Hive and had some troubles working with Ant. For this reason, I bought the book "Ant in Action" and I'll share some thing I've learned with it, and some other experiences I had working with Ant.

Ant is a tool written in Java to build Java. By build I mean compile, run, package it and even test it.
It is designed for helping software teams develop big projects, automating tasks of compiling code.

To run Ant in you project you should have a build.xml file that describes how the projects should be built.
There should be one build per project, except if the project is too big. Then you might have subprojects with "sub build" files. These sub builds are coordinated by the main build file.

The goals of the build are listed as targets. You can have, for example, a target init that creates initial necessary directories.

  
  

You can also have a target compile that depends on the previous init target, and compiles the Java code.

 

After installing Ant, if you run the command:

$ant -p

you will see all the available targets in your project. These are all tests you can perform in your project.

In build.xml, inside of each target tag, you should see a command. In the compile target you should probably see a javac command. That is the action Ant is going to execute to perform the appropriate task.

To test, Ant uses JUNit, a unit test framework that verifies if your software components are working individually.
JUnit is API that facilitates writing Java test cases, whose execution can be fully automated. To use it, you just need to download a junit.jar file and set it in your Ant path. In my case, it was just a matter of adding it to the /usr/share/ant/lib/ directory.

Writing a test for JUnit involves three steps:

• Create a subclass of junit.framework.TestCase.
• Provide a constructor, accepting a single String name parameter, which calls super (name).
• Write some public no-argument void methods prefixed by the word test.

See the example:

public class SimpleTest extends TestCase {
  public SimpleTest(String s) {
    super(s);
  }
  public void testCreation() {
    Event event=new Event();
  }
}
The only part that actually  tests anything in this program is the testCreation() method, which is simply going to try to create an Event.
Beware that methods without the "test" prefix are ignored. Also your methods shouldn't have any arguments neither a return type.

Pay attention that this regards to JUnit 3. There are several differences between JUnit 3 and 4. You can check some of them here.
Just so you know, if the testcase fails, you will be presented with a junit.framework.AssertionFailedError.

To run your tests with ant, you should add a test target, with a <junit> tag.
A build.xml example would be:

  
  
    
    
  

It should work by just typing:

$ant test-basic

Comments

Popular posts from this blog

Apache Mahout

Slope One

Error when using smooth.spline