-1

I am at a loss on this one and I'm not really sure how to explain it, but I'll give it my best shot.

I've got this project where the client wants me to create a competition. One person has to upload his Java code, another person had to upload his JUnit tests. My application is supposed to take these 2 files and run the uploaded tests on the uploaded code.

Now I know how to make it possible for the users to upload their files, but I haven't got the slightest clue how I can actually run the tests. Could anyone help me out with this one?

Here is the code I've got so far:

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
    }
BenMorel
  • 34,448
  • 50
  • 182
  • 322
RemcoW
  • 4,196
  • 1
  • 22
  • 37
  • possible duplicate of [How do I programmatically compile and instantiate a Java class?](http://stackoverflow.com/questions/2946338/how-do-i-programmatically-compile-and-instantiate-a-java-class) – Joe Apr 20 '14 at 03:53
  • I've been trying to get it working using that all day, but I can't get it too work. I am using the JDK but it keeps failing too load the class. – RemcoW Apr 21 '14 at 16:59
  • Does this question help? [How do I run JUnit tests from inside my java application?](http://stackoverflow.com/questions/2543912/how-do-i-run-junit-tests-from-inside-my-java-application) – Jarrod Dixon May 20 '14 at 21:45

1 Answers1

0

Well your code snippet is not related at all to your problem.

What you have to do is :

  1. compile the class
  2. compile the unit tests class
  3. load both in the same class loader
  4. execute the tests

In order to execute junit tests programatically you have to do :

JUnitCore junit = new JUnitCore();
Result result = junit.runClasses(testClasses); // testClasses is the array of test classes to be executed
Kraal
  • 2,779
  • 1
  • 19
  • 36