Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在单独的代码上运行单元测试_Java_Unit Testing_Junit_Automated Tests - Fatal编程技术网

Java 在单独的代码上运行单元测试

Java 在单独的代码上运行单元测试,java,unit-testing,junit,automated-tests,Java,Unit Testing,Junit,Automated Tests,我在这一点上不知所措,我真的不知道如何解释它,但我会尽我最大的努力 我有一个项目,客户希望我创造一个竞争对手。一个人必须上传他的Java代码,另一个人必须上传他的JUnit测试。 我的应用程序应该获取这2个文件,并在上传的代码上运行上传的测试 现在我知道了如何让用户上传他们的文件,但我一点也不知道如何运行测试。有人能帮我解决这个问题吗 以下是我目前掌握的代码: public void actionPerformed(ActionEvent e) { if (e.getSource()

我在这一点上不知所措,我真的不知道如何解释它,但我会尽我最大的努力

我有一个项目,客户希望我创造一个竞争对手。一个人必须上传他的Java代码,另一个人必须上传他的JUnit测试。 我的应用程序应该获取这2个文件,并在上传的代码上运行上传的测试

现在我知道了如何让用户上传他们的文件,但我一点也不知道如何运行测试。有人能帮我解决这个问题吗

以下是我目前掌握的代码:

public void actionPerformed(ActionEvent e) 
{
    if (e.getSource() == codeB)
    {
        //Get the code
        File dir = new File("..");
        fc.setCurrentDirectory(dir);
        int returnVal = fc.showOpenDialog(this);
        if (returnVal == fc.APPROVE_OPTION)
        {
            codeF = fc.getSelectedFile();
            codeV.setText(codeF.getPath());
        }
        else
        {
            System.out.println("Cancel");
        }
    }

    if (e.getSource() == testB)
    {
        //Get the test
        File dir = new File("..");
        fc.setCurrentDirectory(dir);
        int returnVal = fc.showOpenDialog(this);
        if (returnVal == fc.APPROVE_OPTION)
        {
            testF = fc.getSelectedFile();
            testV.setText(testF.getPath());
        }
        else
        {
            System.out.println("Cancel");
        }
    }

    if (e.getSource() == run)
    {
        //Run the tests on the code
    }

您的代码片段与您的问题完全无关

你要做的是:

  • 编译类
  • 编译单元测试类
  • 在同一类加载器中加载这两个类
  • 执行测试
  • 为了以编程方式执行junit测试,您必须执行以下操作:

    JUnitCore junit = new JUnitCore();
    Result result = junit.runClasses(testClasses); // testClasses is the array of test classes to be executed
    

    可能是复制品我整天都在试着让它工作,但我不能让它太工作。我正在使用JDK,但它总是失败,加载类。这个问题有用吗?