Pregunta

Currently my class looks like

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = {DemographicAttributesManager.class})
public class DemographicAttributesManagerTest {

    @Autowired
    private DemographicAttributesManager demographicAttributesManager;

    @Test
    public void testGetGenders() throws Exception {
        final Map<Integer, String> genders = demographicAttributesManager.getGenders();
        assertEquals(3, genders.size());
    }
}

What I don't like?

  1. 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?

  1. I have to type

@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = {DemographicAttributesManager.class}) telling which classes to include and that it is annotation driven test.

Is there a way to set default to package scanning for classes and default that I do not have to put AnnotationConfigContextLoader?

Thanks

¿Fue útil?

Solución

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)

Otros consejos

@RunWith and @ContextConfiguration(loader=...) can be inherited from a base test class. I don't know how to avoid @ContextConfiguration(classes=...) per test class. But I don't think that getting rid of it is a very useful feature: I prefer to have true unit tests with mockito and without @RunWith(spring) and to have true integration tests that would load the whole app context (which will be cached by spring among all the tests).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top