Saturday, August 4, 2012

Testing an ant Task

I had to test the SchemaCrawler ant task. Some research shows

  1. Include the ant testunit jar - in a Maven project you can add a dependency such as
    <dependency>
    <groupId>org.apache.ant</groupId>
    <artifactId>ant-testutil</artifactId>
    <version>1.8.4</version>
    <scope>test</scope>
    </dependency>

  1. Extend your test case from org.apache.tools.ant.BuildFileTest
  2. Write a setup like
  @Override
  public void setUp()
    throws Exception
  {
    configureProject( "./build.xml" );
  }
  1. Write your test cases, with the methods starting with "test" (as in the classic JUnit)
  public void testAntTask()
    throws Exception
  {
    final File testOutputFile = File.createTempFile("schemacrawler.",
                                                    ".test");
    setAntProjectProperty("outputfile", testOutputFile.getAbsolutePath());
    executeTarget("ant_task_test");

    System.out.println(getFullLog());
    System.out.println(getOutput());
    // some asserts here
  }

  1. You can set properties with the utility method
  private void setAntProjectProperty(final String name, final String value)
  {
    final Project antProject = getProject();
    antProject.setProperty(name, value);
  }

No comments:

Post a Comment