对分离的代码运行单元测试

时间:2014-04-19 12:13:27

标签: java unit-testing junit automated-tests

我对此感到茫然,我不确定如何解释它,但我会尽力给它。

我有一个客户希望我创建比赛的项目。一个人必须上传他的Java代码,另一个人必须上传他的JUnit测试。 我的应用程序应该采用这两个文件并在上传的代码上运行上传的测试。

现在我知道如何让用户上传他们的文件,但我还没有一丝线索如何实际运行测试。任何人都可以帮我解决这个问题吗?

以下是我到目前为止所获得的代码:

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
    }

1 个答案:

答案 0 :(得分:0)

您的代码段根本与您的问题无关。

你要做的是:

  1. 编译课程
  2. 编译单元测试类
  3. 在同一个类加载器中加载
  4. 执行测试
  5. 为了以编程方式执行junit测试,你必须这样做:

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