我正在尝试加速我们环境中的集成测试。我们所有的课程都是自动装配的。在我们的applicationContext.xml文件中,我们定义了以下内容:
I am trying to speed up the Integration tests in our environment. All our classes are autowired. In our applicationContext.xml file we have defined the following:
<context:annotation-config/> <context:component-scan base-package="com.mycompany.framework"/> <context:component-scan base-package="com.mycompany.service"/> ...additional directories我注意到Spring正在扫描上面指出的所有目录然后迭代每个bean并缓存每个bean的属性。 (我从春季开始查看DEBUG消息)
I have noticed that Spring is scanning all directories indicated above and then iterates over each bean and caches the properties of each one. (I went over the DEBUG messages from spring)
因此,以下测试大约需要14秒才能运行:
As a result, the following test takes about 14 seconds to run:
public class MyTest extends BaseSpringTest { @Test def void myTest(){ println "test" } }有没有办法延迟加载配置?我尝试添加 default-lazy-init =true但这不起作用。
Is there any way to lazy load the configuration? I tried adding default-lazy-init="true" but that didn't work.
理想情况下,仅限测试所需的bean被实例化。
Ideally, only the beans required for the test are instantiated.
提前感谢。
更新:我之前应该说过,我不想为每个测试都有一个上下文文件。我也不认为只有测试的上下文文件才有效。 (此测试上下文文件最终将包含所有内容)
Update: I should have stated this before, I do not want to have a context file for each test. I also do not think one context file for just the tests would work. (This test context file would end up including everything)
推荐答案如果您真的想加快应用程序上下文,禁用<组件扫描,并在运行任何测试之前执行以下例程
If you really want to speed up your application context, disable your <component-scan and performs the following routine before running any test
Resource resource = new ClassPathResource(<PUT_XML_PATH_RIGHT_HERE>); // source.xml, for instance InputStream in = resource.getInputStream(); Document document = new SAXReader().read(in); Element root = document.getRootElement(); /** * remove component-scanning */ for ( Iterator i = root.elementIterator(); i.hasNext(); ) { Element element = (Element) i.next(); if(element.getNamespacePrefix().equals("context") && element.getName().equals("component-scan")) root.remove(element); } in.close(); ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(true); for (String source: new String[] {"com.mycompany.framework", "com.mycompany.service"}) { for (BeanDefinition bd: scanner.findCandidateComponents(source)) { root .addElement("bean") .addAttribute("class", bd.getBeanClassName()); } } //add attribute default-lazy-init = true root.addAttribute("default-lazy-init","true"); /** * creates a new xml file which will be used for testing */ XMLWriter output = new XMLWriter(new FileWriter(<SET_UP_DESTINATION_RIGHT_HERE>)); output.write(document); output.close();除此之外,启用< context:annotation-config />
Besides that, enable <context:annotation-config/>
由于您需要在运行任何测试之前执行上述例程,您可以创建一个抽象类,您可以在其中运行以下内容
As you need to perform the routine above before running any test, you can create an abstract class where you can run the following
设置Java系统属性以测试环境,如下所示
Set up a Java system property for testing environment as follows
-Doptimized-application-context=false和
public abstract class Initializer { @BeforeClass public static void setUpOptimizedApplicationContextFile() { if(System.getProperty("optimized-application-context").equals("false")) { // do as shown above // and System.setProperty("optimized-application-context", "true"); } } }现在,对于每个测试类,只需扩展Initializer
Now, for each test class, just extends Initializer
更多推荐
自动装配时,Spring集成测试速度很慢
发布评论