如何编译JUnit测试类?

时间:2015-03-23 09:58:32

标签: java junit classpath

我可以使用以下代码轻松编译任何Java类:

    System.out.println("Compiling student source files");
    Path destinationCompiledClassFile = Paths.get("/submissions/" + moduleCode + "/Assessment" + assessmentNumber + "/" + studentId + "/Submission"
            + submissionNumber + "/" + assessmentFolderName + "/" + "build/classes/");

    Path classPath = Paths.get("/submissions/" + moduleCode + "/Assessment" + assessmentNumber + "/" + studentId + "/Submission"
            + submissionNumber + "/" + assessmentFolderName + "/" + "build/classes/oojavabootcamp" + "/");
    Path javaFileToCompile = Paths.get("/submissions/" + moduleCode + "/Assessment" + assessmentNumber + "/" + studentId + "/Submission"
            + submissionNumber + "/" + assessmentFolderName + "/" + "src/OOJavaBootcamp");

    for (String fileToCompile : filesToCompile) {
        String compileCommand = "cmd /c javac -d \"" +  destinationCompiledClassFile
                + "\" -cp \"" + classPath
                + "\" \"" + javaFileToCompile + "/"
                + fileToCompile + ".java\" >> \""
                + submissionPathString + "/"
                + compilerOutputFileName + "\" 2>&1";

我试图通过在类路径中添加JUnit JAR文件来在简单的Java应用程序中编译test.class文件。这应该将新编译的test.class文件添加到指定的-d目录中。我删除了此路径中的现有文件' C:\ submissions \ SOFT222 \ Assessment1 \ 10403435 \ Submission20 \ OO Java Bootcamp - Java和NetBeans \ build \ test \ classes \ oojavabootcamp'并且需要新编译的文件。

static String testFileName = "OOJavaBootcampTest";
final static String jUnitFileName = "junit-4.11.jar";

 System.out.println("Compiling student test source file");

    String compileCommand = "cmd /c javac -d \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\build\\test\\classes"
            + "\" -cp \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\build\\test\\classes\\oojavabootcamp\"" + "; "
            + "\"C:\\submissions\\SOFT222\\" + jUnitFileName
            + "\"; \"" + "C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\test\\oojavabootcamp" + "/"
            + testFileName + ".java\"; >> \"" + ""
            + "C:\\Users\\aookpidi\\Desktop\\testCompilerOutput.txt" + "\" 2>&1";

    System.out.println("Compile student source command: "
            + compileCommand);

我一直得到的编译错误是:

     javac: invalid flag: C:\submissions\SOFT222\junit-4.11.jar;
     Usage: javac <options> <source files>
     use -help for a list of possible options

1 个答案:

答案 0 :(得分:1)

请检查另一个问题的答案:
https://stackoverflow.com/a/2096321/4211402

基本上,它指出在Windows中构建CLASSPATH时,您可能需要将\*添加到目录的末尾。

我希望它有所帮助。