Hier erfahren Sie, wie Sie eine djUnit-Aufgabe in Ant ausführen. Eclipse ist neu und Sie können djUnit nicht mehr verwenden, aber ich denke, es ist nützlich, wenn Sie weiterhin djUnit-Tests in Ihrem Projekt verwenden möchten.
Ant 1.9.7 jp.co.dgic.eclipse.jdt.djunit_3.5.x_0.8.6.zip jUnit4 Eclipse Release 4.7.0 (Oxygen)
Erstellen Sie zwei Testklassen und zwei Testklassen und kopieren Sie die erforderlichen Gläser aus Ant, djUnit und jUnit in den Ordner test / lib.
Original | jar | Bemerkungen |
---|---|---|
Ant | ant-junit4.jar | |
djUnit | asm-3.1.jar | asm-1.5.3.jar、asm-2.2.1.Kein Glas erforderlich |
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 |
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()));
}
}
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 erstellt den Prototyp mit der Exportfunktion von Eclipse und fügt die Aufgabe von djUnit hinzu. Die Djunit-Task "usenoverify =" true "" ist erforderlich, wenn Sie in Java 8 ausgeführt werden.
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>
Es kann über Eclipse oder die Befehlszeile ausgeführt werden. Verwenden Sie jedoch dieselbe Ant wie die Version von ant-junit4.jar, die Sie in test / lib kopiert haben. Das Ergebnis der Ausführung über die Befehlszeile wird unten angezeigt.
Ein Bericht über die Berichterstattung wird ebenfalls erstellt, aber es ist seltsam ...
Ich habe diesen Artikel geschrieben, weil es nicht viele Informationen zu djUnit gibt, weil es alt ist. Ich denke, es gibt einige Punkte, die korrigiert werden müssen. Wenn Sie also etwas bemerken, würde ich es begrüßen, wenn Sie darauf hinweisen könnten.
Remote-Debugging in Eclipse. Erstellen Sie das jvmarg der djunit-Task in build.xml wie folgt: Du musst -ea nicht haben.
build.xml
<jvmarg line="-ea -agentlib:jdwp=transport=dt_socket,suspend=y,server=y,address=8000"/>
Wenn es ausgeführt wird, stoppt es in der Mitte, wie unten gezeigt.
Wählen Sie als Nächstes Testprojekt aus Eclipse aus, wählen Sie [Rechtsklick] → [Debug] → [Debug-Konfiguration] → [Remote-Java-Anwendung], geben Sie wie in der Abbildung gezeigt ein und drücken Sie die Debug-Taste. Sie können jetzt debuggen.
Vergessen Sie nicht, den Haltepunkt festzulegen.
Recommended Posts