0

I get this error:

/student/src/reflection/tester/TestRunnerTests.java:21: error: unreported exception Exception; must be caught or declared to be thrown testRunner.runTests(testClassNames);

So it shows that the error must be on testRunner.runTests(testClassNames); line in this class:

public class TestRunnerTests {

    @Test
    public void runsTestsFromDecoupledFiles() throws Exception {

        List<String> testClassNames = List.of(
                "reflection.tester.ExampleTests1", "reflection.tester.ExampleTests2");

        TestRunner testRunner = new TestRunner();

        testRunner.runTests(testClassNames);

        String result = testRunner.getResult();

        assertThat(result, containsString("test1() - OK"));
        assertThat(result, containsString("test2() - FAILED"));
        assertThat(result, containsString("test3() - OK"));
        assertThat(result, containsString("test4() - FAILED"));
        assertThat(result, containsString("test5() - OK"));
        assertThat(result, containsString("test6() - FAILED"));

        assertThat(result, not(containsString("helperMethod()")));
    }
}

My actual code looks like this:

public class TestRunner{
   List<String> result = new LinkedList<>();
   List<String> annotatedMethods = new LinkedList<>();

   public void runTests(List<String> testClassNames) throws Exception {
       for (String test: testClassNames) {
           Class<?> aClass = Class.forName(test);
           getClassMethods(aClass);
       }
   }
.....
} 

Tried something like this, but it says A catch statement that catches an exception only to wrap it in a new instance of the same type of exception and throw it should be avoided

try{
   for (String test: testClassNames) {
        Class<?> aClass = Class.forName(test);
        getClassMethods(aClass);
   }  
} catch (Exception e){
   throw new Exception(e);
}

How should I solve it?

Meowz
  • 13
  • 2
  • Does this answer your question? [Why do I get "Exception; must be caught or declared to be thrown" when I try to compile my Java code?](https://stackoverflow.com/questions/908672/why-do-i-get-exception-must-be-caught-or-declared-to-be-thrown-when-i-try-to) – Abra Apr 26 '21 at 08:50

1 Answers1

0

If you throw an Exeption you have to catch it anywhere. So you need to wrap the call of the method "runTests" into a try-catch

(General: You need to catch a thrown exception outside of this instance)

you wrote: testRunner.runTests(testClassNames);

try: try{testRunner.runTests(testClassNames);} catch (Exception e) {...}

  • I tried it above as I showed, but it still doesn't work :/ – Meowz Apr 26 '21 at 07:25
  • No you didn't try, if i see right in the first code snippet you wrote: `testRunner.runTests(testClassNames);` try: `try{testRunner.runTests(testClassNames);} catch (Exception e) {...}` – Timon Gärtner Apr 26 '21 at 07:28
  • And when you tried it you did just throw a new exception: `throw new Exception(e);` But you need to catch this Exception anywhere outside this instance, don't throw a new exception, either write nothing in it or do something, but dont throw a new exception – Timon Gärtner Apr 26 '21 at 07:35
  • If your run into more problems pls let me know – Timon Gärtner Apr 26 '21 at 07:40