How to make unit test custom ant task?

I am writing a custom ant task that extends the task. I use the log () method in the task. What I want to do is use unit test when defining a task, but I don’t know how to configure the context for running the task to initialize the task, as if it were running in ant.

This is a custom task:

public class CopyAndSetPropertiesForFiles extends Task { public void execute() throws BuildException { log("CopyAndSetPropertiesForFiles begin execute()"); log("CopyAndSetPropertiesForFiles end execute()"); } } 

This is the unit test code:

 CopyAndSetPropertiesForFiles task = new CopyAndSetPropertiesForFiles(); task.execute(); 

When the code runs as a test, it throws a NullPointerException when the log is called.

 java.lang.NullPointerException at org.apache.tools.ant.Task.log(Task.java:346) at org.apache.tools.ant.Task.log(Task.java:334) at uk.co.tbp.ant.custom.CopyAndSetPropertiesForFiles.execute(CopyAndSetPropertiesForFiles.java:40) at uk.co.tbp.ant.custom.test.TestCopyAndSetPropertiesForFiles.testCopyAndSetPropertiesForFiles(TestCopyAndSetPropertiesForFiles.java:22) 

Does anyone know a way to provide context or stubs or something similar to a task?

Thanks,

Rob.

Accepted answer from Abarax. I managed to call task.setProject (new project ()); Now the code is executed OK (except that the log is not logged in the console - at least I can use the code :-)).

+6
junit ant
source share
3 answers

Looking at the Ant source code, these are two corresponding classes: ProjectComponent and Task

You call the log method from the task:

 public void log(String msg) { log(msg, Project.MSG_INFO); } 

What are the challenges:

 public void log(String msg, int msgLevel) { if (getProject() != null) { getProject().log(this, msg, msgLevel); } else { super.log(msg, msgLevel); } } 

Since you do not have a set of projects, it will call "super.log (msg, msgLevel)"

 public void log(String msg, int msgLevel) { if (getProject() != null) { getProject().log(msg, msgLevel); } else { // 'reasonable' default, if the component is used without // a Project ( for example as a standalone Bean ). // Most ant components can be used this way. if (msgLevel <= Project.MSG_INFO) { System.err.println(msg); } } } 

Seems like this could be your problem. Your project needs a project context.

+1
source share

Or even better, separate the task object from the logic (allows you to call TaskImpl) inside the task - so that you can transfer your own dependencies (for example, a registrar). Then, instead of testing the task object, you check TaskImpl β†’, which you can transfer in the logger, and any other strange bits and fragments that may be required to do your job. Unit testing is then a matter of ridicule of dependencies.

+8
source share

Ant has a convenient BuildFileTest class that extends the JUnit TestCase class. You can use it to verify the behavior of individual targets in the assembly file. Using this, you take care of the whole annoying context.

There is Test the Task in the Apache Ant Writing Tasks tutorial that describes this.

+1
source share

All Articles