简体   繁体   English

如何仅在 JUnit 春季测试中自动装配特定类?

[英]How to only autowire a specific class in JUnit spring test?

I want to write a kind of integration test, but only for a specific class.我想写一种集成测试,但只针对特定的类。 Which means I want all fields in that class to be automatically wired, but neglect any classes inside the same directory.这意味着我希望该类中的所有字段都自动连接,但忽略同一目录中的任何类。

Is that possible?那可能吗?

Example:例子:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {TestConfig.class})
public class JunitTest {
    @Autowired
    private MyService service;
}

//how to only scan that specific class?
@ComponentScan(basePackageClasses = {MyService.class, MyInjectedService.class})
@Configuration
public class TestConfig {

}


@Service
public class MyService {
    @Autowired
    private MyInjectedService service;
}

Here I want spring to neglect any classes in the same directories as the both basePackageClasses mentioned.在这里,我希望 spring 忽略与提到的两个basePackageClasses相同目录中的任何类。

The classes attributes supported by @ContextConfiguration and @SpringApplicationConfiguration are only required to reference annotated classes ; @ContextConfiguration@SpringApplicationConfiguration支持的classes属性只需要引用注解的类 they do not have to be @Configuration classes.它们不必是@Configuration类。

So with that in mind, you should be able to completely forgo component scanning and simply declare your two annotated components as follows:因此,考虑到这一点,您应该能够完全放弃组件扫描并简单地声明您的两个带注释的组件,如下所示:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {MyService.class, MyInjectedService.class})
public class JUnitTest {
    @Autowired
    private MyService service;

    // ...
}

Having said that, if the above is your goal, it might not make any sense to use Spring Boot's testing support.话虽如此,如果以上是您的目标,那么使用 Spring Boot 的测试支持可能没有任何意义。 In other words, if MyService and MyInjectedService do not rely on any features of Spring Boot, you can safely replace @SpringApplicationConfiguration with @ContextConfiguration .换句话说,如果MyServiceMyInjectedService不依赖 Spring Boot 的任何特性,您可以放心地将@SpringApplicationConfiguration替换为@ContextConfiguration

Regards,问候,

Sam (author of the Spring TestContext Framework ) Sam( Spring TestContext 框架的作者)

You could make use of filters to customize scanning.您可以使用过滤器来自定义扫描。 There you can extend the ComponentScan Annotation with Attributes like this:在那里,您可以使用如下属性扩展 ComponentScan Annotation:

@ComponentScan(
basePackages = {"com.my.package"}, 
useDefaultFilters = false,
includeFilters = {
    @Filter(type = FilterType.ASSIGNABLE_TYPE, value = {MyService.class, MyInjectedService.class})
})

You can use @Import annotation.您可以使用@Import注释。 It's more readable and shorter than using the @ComponentScan annotation.它比使用@ComponentScan注释更具可读性和更短。

import org.springframework.context.annotation.Import;

@Import(value = {MyService.class, MyInjectedService.class})
public class JUnitTest {
    @Autowired
    private MyService service;

    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM