我有一个支持多个数据库(MySQL、Oracle、Server、Postgre)的应用程序。我想在Spring中配置多个配置文件,以便在多个数据库上运行单元测试(我使用H2作为我的主内存数据库)。每当我通过maven运行build时,它就会在多个数据库上运行所有单元测试。目前,每当我通过maven构建项目时,我就只能在一个数据库上运行单元测试。如何配置来实现这一点?
发布于 2014-01-19 01:22:18
我将使用自定义的JUnit规则:
public class MultipleSpringConfigRule implements TestRule
{
private Object _testObject;
private String[] _configLocations;
public MultipleSpringConfigRule(Object testObject, String... configLocations)
{
_testObject = testObject;
_configLocations = configLocations;
}
public Statement apply(final Statement base, Description description)
{
return new Statement()
{
@Override
public void evaulate() throws Throwable
{
for (String configLocation : _configLocations)
{
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext(configLocation);
context.getAutowireCapableBeanFactory().autowireBean(_target);
context.start();
try
{
// executes Before methods, current Test method, and After
// methods.
base.evaluate();
} finally
{
context.close();
}
}
}
};
}
}将规则添加到任何需要针对多个spring配置运行的测试类:
public class MyTests
{
@Rule
public MultipleSpringConfigRule _myRule = new MultipleSpringConfigRule(
this,
"path/to/mysql/config.xml",
"path/to/oracle/config.xml",
"path/to/sqlserver/config.xml",
"path/to/postgre/config.xml");
@Autowired
private MyRepository _myRepository;
...
}https://stackoverflow.com/questions/21211835
复制相似问题