简体   繁体   English

如何编译一个JUnit测试类?

[英]How to compile a JUnit test class?

I can easily compile any Java classes using this code: 我可以使用以下代码轻松编译任何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";

I am trying to compile the test.class file in a simple Java application by adding JUnit JAR file in the classpath. 我试图通过在类路径中添加JUnit JAR文件来在一个简单的Java应用程序中编译test.class文件。 This should add the new compiled test.class file to the specified -d directory. 这应该将新的编译后的test.class文件添加到指定的-d目录中。 I have deleted the existing file in this path 'C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission20\\OO Java Bootcamp - Java and NetBeans\\build\\test\\classes\\oojavabootcamp' and need the new compiled file here. 我已删除此路径'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);

The compiler error I keep getting is: 我不断收到的编译器错误是:

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

Please check this answer to another question: 请检查此答案是否为另一个问题:
https://stackoverflow.com/a/2096321/4211402 https://stackoverflow.com/a/2096321/4211402

Basically, it points out that you may need to add \\* to the end of directories in building CLASSPATH in Windows. 基本上,它指出在Windows中构建CLASSPATH时可能需要在目录末尾添加\\*

I hope it helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM