简体   繁体   English

使用弹簧配置文件时自动将bean装配到TestNG测试的问题

[英]Problems autowiring beans to TestNG test when using spring profiles

I am currently polishing a test framework we have. 我目前正在完善我们拥有的测试框架。 For the current needs, we must support multiple spring profiles, and run our tests multiple times, each time with a different profile. 对于当前的需求,我们必须支持多个弹簧曲线,并多次运行测试,每次使用不同的曲线。 Each profile targets separate test environments, and thus different sets of tests, with different logic may be executed. 每个配置文件针对单独的测试环境,因此可以执行具有不同逻辑的不同测试集。

I am having a test class like this: 我正在上这样的测试课:

@ContextConfiguration(locations = { "classpath:META-INF/test-context.xml" })
public class Test extends AbstractTestNGSpringContextTests {
    @Autowired
    ProfileSpeciticBean profileSpecificBean;

    ...
}

Here, ProfileSpecificBean is an interface, that is implemented by separate classes. 在这里, ProfileSpecificBean是一个接口,由单独的类实现。 The actual implementation to be injected is determined by the active Spring profile, and I am using Spring XML contexts. 实际要注入的实现由活动的Spring概要文件确定,我正在使用Spring XML上下文。 I am building the project with Maven, using the -Dspring.profiles.active=profileName command, thus expecting the tests to catch the passed profile. 我正在使用-Dspring.profiles.active=profileName命令使用Maven构建项目,因此希望测试能够捕获通过的概要文件。

However, the current test fails with this error within the full stacktrace: 但是,当前测试失败,并在完整的堆栈跟踪中出现此错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ProfileSpeciticBean found for dependency: expected at least 1 bean which qualifies as autowire candindate, found 0 org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到依赖项ProfileSpeciticBean类型的合格Bean:预计至少有1个合格的autowire候选bean,找到0

After some reaearch on this topic, I found that the AbstractTestNGSpringContextTests expects an @ActiveProfiles annotation on top of the test class. 在对该主题进行了一些重新研究之后,我发现AbstractTestNGSpringContextTests在测试类的顶部期望有一个@ActiveProfiles批注。 So, this code works: 因此,此代码有效:

 @ContextConfiguration(locations = { "classpath:META-INF/test-context.xml" })
 @ActiveProfiles("profile1")
 public class Test extends AbstractTestNGSpringContextTests ...

The problem with this is: I want to avoid hard-coding the profile name in my classes. 问题是:我想避免在类中对配置文件名称进行硬编码。 I need to run the same test class for different profiles, by only altering the command-line script. 我只需要更改命令行脚本,即可针对不同的配置文件运行相同的测试类。

Is the above possible? 以上可能吗? Is there any way to make TestNG aware of the command-line profile, and re-use the same test? 有什么方法可以使TestNG知道命令行配置文件,并重新使用同一测试? I need to avoid both duplicating code and configuration to make my tests run, so making two Test classes for each profile is not what I want. 我需要避免重复代码和配置来运行测试,因此我不希望为每个配置文件创建两个Test类。

To get more precise answer I suggest you add stacktrace and the piece of you main configuration (where you declared beans that is supposed to be replaced by test beans). 为了获得更精确的答案,我建议您添加stacktrace和您的主要配置(在其中声明了应该由测试bean替换的bean)。

Here is the general idea: 这是一般的想法:

let's say you want to change PropertyPlaceholderConfigurer depending on your profile. 假设您要根据个人资料更改PropertyPlaceholderConfigurer。

Steps: 脚步:

  1. You create you main-config.xml that contains PropertyPlaceholderConfigurer and mark it with profile="default" 您创建包含PropertyPlaceholderConfigurer的main-config.xml并将其标记为profile =“ default”
  2. You create you test-config.xml with test implementation of PropertyPlaceholderConfigurer 使用PropertyPlaceholderConfigurer的测试实现创建test-config.xml
    (don't forget to mark test-config.xml with profile="MyTestProfile" or mark only PropertyPlaceholderConfigurer with profile="MyTestProfile) (不要忘记用profile =“ MyTestProfile”标记test-config.xml或仅用profile =“ MyTestProfile标记PropertyPlaceholderConfigurer)
  3. Than you import both test-config.xml and main-config.xml to your tests 比将test-config.xml和main-config.xml都导入到测试中

 @ContextConfiguration(locations = { "classpath:META-INF/main-config.xml","classpath:META-INF/test-config.xml" })
 @ActiveProfiles("MyTestProfile")
 public class Test extends AbstractTestNGSpringContextTests {}

It should work. 它应该工作。 Good luck. 祝好运。

Try following How to set JVM parameters for Junit Unit Tests? 尝试遵循如何为Junit单元测试设置JVM参数? to set the system variables for the VM that actually runs the tests - it's not the same one as the one that runs maven. 为实际运行测试的虚拟机设置系统变量-与运行maven的虚拟机不同。

Set your profile there. 在此设置您的个人资料。

You can use a maven system parameter to set that from the invocation of maven (or use maven profiles). 您可以使用maven系统参数从maven调用中进行设置(或使用maven配置文件)。

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

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