Search code examples
javatestingjunit

Running only the tests with @MyTest inside java application


I have a TestRunner class:

public class TestRunner {

    private String result = "";

    public void runTests(List<String> testClassNames) {
        for (String testClassName : testClassNames) {

        }
    }

    public String getResult() {
        return result;
    }
}

Then I have two test classes and I should run only the tests with @MyTest from both classes. getResult() should essentialy return "exampleTest() - OK" or "exampleTest() - FAILED", depending on if the test passes. Test class looks like this:

public class ExampleTests1 {

    @MyTest (expected = IllegalStateException.class)
    public void test1() {
        throw new IllegalStateException();
    }

    @MyTest (expected = IllegalStateException.class)
    public void test2() {
        throw new RuntimeException();
    }
}

I tried to do it with JUnitCore and Result, but couldn't find a way to get the test method name to add to the result. Is there another way?


Solution

  • You'll have to use reflection in this case. This is kind of how JUnit works internally:

    Since this sounds as a homework/assigment for educational purposes (If you're developing a "real" application - then just use JUnit or TestNG and don't write anything like this by yourself :) ), I won't provide a full solution however this is what you should do:

    1. For each class identified by a className you should get a java.lang.Class that describes this class name. You can use Class.forName(...)
    2. Then you should get all the methods of that class (by reflection) and for each method run the following:

    2.1 Check whether the method is marked with an annotation @MyTest. If it doesn't - don't handle it

    2.2 Also check whether the method name starts with test (String has startsWith method)

    2.3 If you found out that the test class contains test methods, then:

    2.3.1 Create an instance of the Test Class (probably you can assume that it has no-ops constructor, then use newInstance())

    2.3.2 Run the method (again by reflection). Check the result / surround the execution call with try/catch block to intercept errors.

    2.3.3 Print the Result as specified in the assignment :)