1. Make sure you already have a MiniJava.java file in the
src folder, defined a public class MiniJava,
which should have a public static void main(String[] args)
method, and it always calls System.exit upon return.
2. Download utils.zip, unzip it, and move
the unzipped utils folder to inside the test
folder. Your project structure should now look like this:
cse401-23sp-group id
├── build.xml
├── ...
├── src/
│ ├── ...
│ └── MiniJava.java
└── test/
├── junit/
├── resources/
└── utils/
├── allow.java
├── CSE401TestUtils.java
├── ExecutionResult.java
├── MiniJavaTestBuilder.java
├── PrintStreams.java
├── README.txt
└── ThrowingRunnable.java
2.1 If you use IntelliJ IDEA, right click on the utils
folder in the IntelliJ IDEA Project panel, select
Mark Directory As, then
Test Sources Root.
See
IntelliJ IDEA | content roots for more info.
3. Modify the build.xml file at the top of level of
your project to compile the newly added files. You should do so by
adding the blue, bolded part below to inside the
compile-test target:
<target name="compile-test" depends="compile">
<javac srcdir="test/utils" destdir="build/classes"
classpath="lib/hamcrest-core-1.3.jar;lib/junit-4.12.jar;lib/java-cup-11b.jar"
debug="true"
includeAntRuntime="false">
</javac>
<javac srcdir="test/junit" destdir="build/classes"
classpath="lib/hamcrest-core-1.3.jar;lib/junit-4.12.jar;lib/java-cup-11b.jar"
debug="true"
includeAntRuntime="false">
</javac>
</target>
4. Next, update the test target in the same build.xml file,
adding
fork="yes" to junit, and<jvmarg value="-Djava.security.manager=allow"/> inside junit.
<target name="test" depends="compile-test">
<junit fork="yes">
<jvmarg value="-Djava.security.manager=allow"/>
<classpath>
...
4.1 If you use IntelliJ IDEA and run the JUnit tests using the
Run button/menu instead of the ant command/window,
see Testing MiniJava in IntelliJ.
5. To support how the testing framework detects the exit status, structure your MiniJava.main in one of two ways:
try ... catch (Exception e) ...
class MiniJava {
public static void main(String[] args) {
try {
...
} catch (Exception e) {
...
}
// only System.exits here
System.exit(...);
}
}
class MiniJava {
public static void main(String[] args) {
try {
...
System.exit(...);
} catch (SecurityException exit) {
throw exit;
} catch (Exception e) {
...
}
}
}
6. Done. You should be able to use your existing
ant commands just like before.