How to run a djUnit task in Ant

Here's how to run a djUnit task in Ant. Eclipse is new and you can no longer use djUnit, but I think it will be useful if you want to continue using djUnit tests in your project.

Operating environment

Ant 1.9.7 jp.co.dgic.eclipse.jdt.djunit_3.5.x_0.8.6.zip jUnit4 Eclipse Release 4.7.0 (Oxygen)

Project structure

Create two test classes and two test classes, and copy the required jars from Ant, djUnit, and jUnit to the test / lib folder.

Original jar Remarks
Ant ant-junit4.jar
djUnit asm-3.1.jar asm-1.5.3.jar、asm-2.2.1.No jar required
asm-attrs-1.5.3.jar
djunit.jar
jakarta-oro-2.0.7.jar
jcoverage-djunit-1.0.5.jar
jUnit junit.jar
hamcrest-core-1.3.jar

Screenshot from 2019-08-25 18-43-41.png

Tested class

HelloService.java


package com.example;

public class HelloService {

	private String name;
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getName() {
		return this.name;
	}
	
	public String getMessage() {
		return String.format("Hello %s!", this.getName());
	}
	
	public void sayHello() {
		System.out.println(this.getMessage());
	}
}

CalculateService.java


package com.example;

public class CalculateService {

	public enum Operation {
		ADD,
		SUBTRACT,
		MULTIPLY,
		DIVIDE
	};

	private int v1;
	private int v2;
	private Operation op;
	
	public void setValue1(int value) {
		this.v1 = value;
	}
	
	public void setValue2(int value) {
		this.v2 = value;
	}
	
	public void setOperation(Operation op) {
		this.op = op;
	}
	
	public float getResult() {
		switch (this.op) {
		case ADD:
			return this.v1 + this.v2;
		case SUBTRACT:
			return this.v1 - this.v2;
		case MULTIPLY:
			return this.v1 * this.v2;
		case DIVIDE:
			if (this.v2 == 0) throw new IllegalArgumentException("value2 must not be zero.");
			return (float)this.v1 / this.v2;
		}
		throw new IllegalArgumentException("operation isn't specified.");
	}
	
	public void calculate() {
		String opString = "?";
		switch (this.op) {
		case ADD:
			opString = "+";
			break;
		case SUBTRACT:
			opString = "-";
			break;
		case MULTIPLY:
			opString = "*";
			break;
		case DIVIDE:
			opString = "/";
			break;
		}
		System.out.println(String.format("%d %s %d = %.3f", this.v1, opString, this.v2, this.getResult()));
	}
}

Test class

HelloServiceTest.java


package com.example;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import org.junit.Before;
import org.junit.Test;

import jp.co.dgic.testing.common.virtualmock.MockObjectManager;

public class HelloServiceTest {

	@Before
	public void init() {
		MockObjectManager.initialize();
	}
	
	@Test
	public void test001() {
		HelloService service = new HelloService();
		service.setName("Japan");
		assertThat(service.getMessage(), is("Hello Japan!"));
		
		MockObjectManager.setReturnValueAtAllTimes(HelloService.class, "getName", "World");
		assertThat(service.getMessage(), is("Hello World!"));
		
		service.sayHello();
		MockObjectManager.assertCalled(HelloService.class, "sayHello");
	}
}

CalculateServiceTest.java


package com.example;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import org.junit.Before;
import org.junit.Test;

import com.example.CalculateService.Operation;

import jp.co.dgic.testing.common.virtualmock.MockObjectManager;

public class CalculateServiceTest {

	@Before
	public void init() {
		MockObjectManager.initialize();
	}
	
	@Test
	public void test001() {
		CalculateService service = new CalculateService();
		service.setValue1(100);
		service.setValue2(50);
		service.setOperation(Operation.ADD);
		service.calculate();
		assertThat(service.getResult(), is(150f));
		MockObjectManager.assertCalled(CalculateService.class, "calculate");
		
		MockObjectManager.setReturnValueAtAllTimes(CalculateService.class, "getResult", 777f);
		service.calculate();
		assertThat(service.getResult(), is(777f));
	}
}

build.xml build.xml creates the prototype with the export function of Eclipse and adds the task of djUnit. The djunit task "usenoverify =" true "" is required when running in Java 8.

build.xml


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="coverage.report" name="testproject">
    <taskdef classpath="./test/lib/djunit.jar" resource="djunittasks.properties"/>
    
    <property environment="env"/>
    <property name="ECLIPSE_HOME" value="../../eclipse/eclipse/"/>
    <property name="src.main" value="./src/main/java"/>
    <property name="test.src.main" value="./test/src/main/java"/>
    <property name="junit.output.dir" value="junit"/>
    <property name="debuglevel" value="source,lines,vars"/>
    <property name="target" value="1.8"/>
    <property name="source" value="1.8"/>
    <path id="testproject.classpath">
        <pathelement location="bin"/>
        <pathelement location="test/lib/ant-junit4.jar"/>
        <pathelement location="test/lib/asm-3.1.jar"/>
        <pathelement location="test/lib/asm-attrs-1.5.3.jar"/>
        <pathelement location="test/lib/jakarta-oro-2.0.7.jar"/>
        <pathelement location="test/lib/jcoverage-djunit-1.0.5.jar"/>
        <pathelement location="test/lib/djunit.jar"/>
        <pathelement location="test/lib/junit.jar"/>
        <pathelement location="test/lib/hamcrest-core-1.3.jar"/>
    </path>
    <target name="init">
        <mkdir dir="bin"/>
        <copy includeemptydirs="false" todir="bin">
            <fileset dir="src/main/java">
                <exclude name="**/*.launch"/>
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
        <copy includeemptydirs="false" todir="bin">
            <fileset dir="test/src/main/java">
                <exclude name="**/*.launch"/>
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
    </target>
    <target name="clean">
        <delete dir="bin"/>
    </target>
    <target depends="clean" name="cleanall"/>
    <target depends="build-subprojects,build-project" name="build"/>
    <target name="build-subprojects"/>
    <target depends="init" name="build-project">
        <echo message="${ant.project.name}: ${ant.file}"/>
        <javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
            <src path="src/main/java"/>
            <src path="test/src/main/java"/>
            <classpath refid="testproject.classpath"/>
        </javac>
    </target>
    <target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
    <target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
        <copy todir="${ant.library.dir}">
            <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
        </copy>
        <unzip dest="${ant.library.dir}">
            <patternset includes="jdtCompilerAdapter.jar"/>
            <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
        </unzip>
    </target>
    <target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
        <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
        <antcall target="build"/>
    </target>
    <target name="HelloServiceTest" depends="build">
        <mkdir dir="${junit.output.dir}"/>
        <djunit printsummary="withOutAndErr" targetsrcdir="${src.main};${test.src.main}" virtualmock="yes" coverage="no" usenoverify="true">
            <formatter type="xml"/>
            <test name="com.example.HelloServiceTest" todir="${junit.output.dir}"/>
            <jvmarg line="-ea"/>
            <classpath refid="testproject.classpath"/>
        </djunit>
    </target>
    <target name="CalculateServiceTest" depends="build">
        <mkdir dir="${junit.output.dir}"/>
        <djunit printsummary="withOutAndErr" targetsrcdir="${src.main};${test.src.main}" virtualmock="yes" coverage="no" usenoverify="true">
            <formatter type="xml"/>
            <test name="com.example.CalculateServiceTest" todir="${junit.output.dir}"/>
            <jvmarg line="-ea"/>
            <classpath refid="testproject.classpath"/>
        </djunit>
    </target>
    <target name="TestAll" depends="build">
        <mkdir dir="${junit.output.dir}"/>
        <djunit printsummary="withOutAndErr" targetsrcdir="${src.main};${test.src.main}" virtualmock="yes" coverage="yes" usenoverify="true">
            <formatter type="xml"/>
            <jvmarg line="-ea"/>
            <classpath refid="testproject.classpath"/>
            <batchtest todir="${junit.output.dir}">
                <fileset dir="./bin">
                    <include name="**/*Test.class"/>
                </fileset>
            </batchtest>
        </djunit>
    </target>
    <target name="coverage.report" depends="TestAll">
        <djunit-coverage-report serFile="./jcoverage.ser" srcdir="${src.main}" destdir="${junit.output.dir}">
            <classpath refid="testproject.classpath"/>
        </djunit-coverage-report>
    </target>
</project>

Run

You can run it from Eclipse or the command line. However, please use the same Ant as the version of ant-junit4.jar that you copied to test / lib. The result of execution from the command line is shown below. Screenshot from 2019-08-25 22-07-04.png

Coverage report

A coverage report is also created, but it's strange ... Screenshot from 2019-08-25 22-14-25.png Screenshot from 2019-08-25 22-15-21.png

Summary

I wrote this article because there is not much information on djUnit because it is old. I think there are some points that need to be corrected, so if you notice something, I would appreciate it if you could point it out.

Postscript 2019/8/27

How to debug remotely in Eclipse. Make the jvmarg of the djunit task in build.xml as follows. You don't have to have -ea.

build.xml


<jvmarg line="-ea -agentlib:jdwp=transport=dt_socket,suspend=y,server=y,address=8000"/>

When executed, it will stop in the middle as shown below. Screenshot from 2019-08-27 22-21-44.png Next, select testproject from Eclipse, select [Right click] → [Debug] → [Debug configuration] → [Remote Java application], enter as shown in the figure, and press the debug button. You can now debug. Don't forget to set the breakpoint. Screenshot from 2019-08-27 22-22-24.png

Recommended Posts

How to run a djUnit task in Ant
How to run Ant in Gradle
How to insert a video in Rails
How to publish a library in jCenter
How to run a job with docker login in AWS batch
Rails: How to write a rake task nicely
How to display a web page in Java
How to add a classpath in Spring Boot
How to create a theme in Liferay 7 / DXP
How to implement a like feature in Rails
How to easily create a pull-down in Rails
How to make a follow function in Rails
How to automatically generate a constructor in Eclipse
How to create a Java environment in just 3 seconds
How to run the SpringBoot app as a service
How to implement a like feature in Ajax in Rails
How to create a Spring Boot project in IntelliJ
How to create a data URI (base64) in Java
How to launch another command in a Ruby program
How to display a browser preview in VS Code
[How to insert a video in haml with Rails]
How to write a date comparison search in Rails
How to store Rakuten API data in a table
How to mock a super method call in PowerMock
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
[Rails 6] How to set a background image in Rails [CSS]
[Rails] How to load JavaScript in a specific view
How to write a core mod in Minecraft Forge 1.15.2
[Ruby/Rails] How to generate a password in a regular expression
How to leave a comment
How to insert a video
How to create a method
How to change a string in an array to a number in Ruby
How to create a placeholder part to use in the IN clause
How to store a string from ArrayList to String in Java (Personal)
How to run npm install on all projects in Lerna
How to select a specified date by code in FSCalendar
How to display a graph in Ruby on Rails (LazyHighChart)
How to add the same Indexes in a nested array
Mapping to a class with a value object in How to MyBatis
How to develop and register a Sota app in Java
How to simulate uploading a post-object form to OSS in Java
How to create a service builder portlet in Liferay 7 / DXP
How to set up a proxy with authentication in Feign
How to use Lombok in Spring
How to find May'n in XPath
How to add columns to a table
How to hide scrollbars in WebView
How to iterate infinitely in Ruby
How to make a Java container
How to master programming in 3 months
How to sign a Minecraft MOD
How to make a JDBC driver
How to run JavaFX on Docker
How to learn JAVA in 7 days
How to install Bootstrap in Ruby
[Java] How to create a folder
How to write a ternary operator
How to use InjectorHolder in OpenAM
[Swift] How to send a notification