1

When I try running JUnit tests, I get the error message

No tests found with test runner 'JUnit 4'

and therefore I have tried solutions from

However, in my case, the difference seems to be that I specify the tests to run on package level instead of folder level.

Running JUnit tests works if I specify a package that directly contains test classes (e.g. com.example.tests.smoketests) but does not work if a higher level package is specified (e.g. com.example.tests).

If I define a test in com.example.tests, it is found and run.

Is there a way to let Eclipse/JUnit find tests in a package and all subpackages?

Community
  • 1
  • 1
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222

1 Answers1

3

Out of the box, there is nothing to do this for you. You can use the Suite annotation to achieve this goal in JUnit4, though this still requires you to manually define a Suite test for each package, and then include all of them in a aggregate Suite test (such they recursively call child Suites).

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import a.sub.package.AnotherSuiteTest.class;

@RunWith(Suite.class)
@Suite.SuiteClasses({
   PackageLocalTestClass1.class,
   PackageLocalTestClass2.class,
   AnotherSuiteTest.class
})
public class JunitTestSuite {   
}

I've played around with building my own utility class before which creates the entire series of tests. This article provides an analogous utility.

Keith
  • 3,079
  • 2
  • 17
  • 26
  • FYI: The code in the link you provided has several issues (finds non-test classes / having several DynamicSuite annotated classes is buggy). – Thomas Weller Sep 18 '15 at 11:53
  • Sorry, @Thomas, I only did a cursory review, and it had the same "look and feel" of what I was trying to convey. :) – Keith Sep 18 '15 at 12:07