In every Test class I have to include
@RunWith(SpringJUnit4ClassRunner.class)
Is there a way to default it? so that every test runs with that class?
No, there is no way to change the default Runner
for JUnit. JUnit defines the default Runner
as org.junit.runners.JUnit4
. So if you want to use a different runner (e.g., SpringJUnit4ClassRunner
), you have to declare it explicitly. That's simply how JUnit works.
However, as someone else pointed out, the @RunWith
annotation is inherited. So you can declare it on a base test class within your project that other test classes extend.
I have to type
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = {DemographicAttributesManager.class})
telling which classes to include and that it is annotation driven test.
You do not need to explicitly declare AnnotationConfigContextLoader
as the loader. Whenever you declare the classes
attribute of @ContextConfiguration
, Spring automatically uses the AnnotationConfigContextLoader
for you. The same is true if you a declare static inner @Configuration
class, thereby making use of the default detection mechanism.
This is all well documented in the corresponding Javadoc as well as in the Testing chapter of the Spring Reference Manual.
Is there a way to set default to package scanning for classes and default that I do not have to put AnnotationConfigContextLoader?
Regarding AnnotationConfigContextLoader
, see above.
Regarding package scanning, no, there is no explicit support for package scanning when using @ContextConfiguration
on its own; however, it is certainly possible to configure component scanning for a given package using a static inner @Configuration
class that is annotated with @ComponentScan
. Naturally, you could also declare that configuration in the same base test class that is annotated with @RunWith
.
Regards,
Sam (author of the Spring TestContext Framework)