I've been scratching my head at this for a while now. I have been looking at all related posts on Stack Overflow and the ones I could find with Google but it was to no avail.
I'm trying to build a Java program that has Mancala.java as main class. The directory structure is as follows: a folder called mancala with one subfolder called test and one subfolder called mancala_test. The test folder has the Mancala.java file and other files and the mancala_test folder contains the JUnit test file called MancalaTest.java. In Eclipse the test file runs, but when running via Ant I get the following error:
init:
compile:
[javac] Compiling 6 source files to C:\Users\[me]\Desktop\build
runjunit:
[junit] Running mancala_test.MancalaTest
[junit] Testsuite: mancala_test.MancalaTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
[junit]
[junit] Caused an ERROR
[junit] mancala_test.MancalaTest
[junit] java.lang.ClassNotFoundException: mancala_test.MancalaTest
[junit] at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:266)
[junit]
[junit] Test mancala_test.MancalaTest FAILED
BUILD SUCCESSFUL
Total time: 1 second
I'm using the following build file in the mancala folder:
<project default="runjunit" name="Compile and run JUnit tests">
<target name="clean">
<delete dir="build"/>
</target>
<target name="clean2">
<delete dir="build"/>
</target>
<target name="init">
<record name="build.log" loglevel="verbose" append="false"/>
</target>
<target name="runjunit" depends="compile">
<junit printsummary="on">
<test name="mancala_test.MancalaTest"/>
<classpath>
<pathelement location="build"/>
</classpath>
<formatter
type="plain"
usefile="false"
/>
</junit>
</target>
<target name="compile" depends="init">
<mkdir dir="build"/>
<javac includeantruntime="false" srcdir="./test" destdir="build"/>
</target>
</project>
Other possible relevant information is that the Mancala.java file contains two static initializers being the GUI and the Mancala class itself (e.g., static Mancala mancala; static GUI gui; and the Mancala_test.java just uses a Mancala mancala = new Mancala() object in each test.
An example of one test:
@Test
public void testAmountOfSeed() {
Mancala mancala = new Mancala();
mancala.divideBoard();
int totalAmountOfSeed = 0;
for (int i = 0; i < mancala.gameBoard.size(); i++) {
totalAmountOfSeed +=mancala.gameBoard.get(i).getSeed();
}
assertTrue("Total amount of seed in initial condition not 48!", totalAmountOfSeed == 48);
}
It probably has something to do with the classpaths (I tried every possible variation I could think of) or the static stuff. I would be very grateful if someone could put me out of my misery.
/edit Directory structure after build: https://i.stack.imgur.com/O2POC.png