我正在处理一个相当小的项目(就依赖性而言),每当我运行单元测试时,JVM加载需要8秒钟,然后在0.2秒内运行实际测试。
我的环境:
Java 8
Spring Tool Suite 3.8.1.RELEASE
JUnit 4
Windows 8
我担心在我的环境中必定会有一些因素造成这种情况需要很长时间,并且我希望有人之前已经看过这个问题并找到了问题的根源并且可能是一个解决方案?
例如。如果我的PATH
环境变量很长,那会有关系吗?
运行JUnit
测试时究竟发生了什么?
我试图运行的实际测试是:
public class TemplateLocationCalculatorTest {
private TemplateLocationCalculator target = new TemplateLocationCalculator();
@Test
public void whenGivenRootReturnIndex(){
Assert.assertEquals("index", target.calculate("/"));
}
}
,目标类是:
public class TemplateLocationCalculator {
public String calculate(String string) {
return "index";
}
}
我希望你在我说这不需要很长时间才能加载时同意我的意见。
答案 0 :(得分:2)
关注suggestion given in the chat我使用了Microsoft Process Monitor,经过很多过滤后,我发现AV软件Avecto DefendPoint正在我的机器上运行,看起来像这是瓶颈。每当我开始测试时,它都会以大约25%的速度运行,这对我来说似乎表明它在我的四个核心之一的单个线程上全速运行。我不是这台机器的管理员所以我无法禁用它来验证这个假设,但一般来说,如果其他人应该看到这个问题,请检查它是否可能是你的防病毒软件。
答案 1 :(得分:-1)
潜在reason可能是启动期间组件扫描和自动装配组件。您可以通过为测试创建单独的config
文件来限制此操作,这些文件限制了组件的搜索空间,如here所述。
在配置中,您可以lazy load beans <beans default-lazy-init="true">
或明确连接bean(详细解释here)
<beans ...>
<!-- this bean will be injected into the OrderServiceTest class -->
<bean id="target" class="TemplateLocationCalculator" />
<!-- other beans -->
</beans>
然后在tests类中,我们指定新的配置文件:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/path/to/test-config.xml" })
public class TemplateLocationCalculatorTest {
@Autowired
private TemplateLocationCalculator target;
@Test
public void whenGivenRootReturnIndex(){
Assert.assertEquals("index", target.calculate("/"));
}
}
@Bean
public class TemplateLocationCalculator {
public String calculate(String string) {
return "index";
}
}