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 :-)).
junit ant
Rob h
source share