JUnit 启动时间很慢
JUnit startup time is slow
我正在做一个相当小的项目(就依赖关系而言),每当我 运行 进行单元测试时,JVM 加载需要 8 秒,然后 运行 0.2秒实测
我的环境:
- Java 8
- Spring 工具套件 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("/"));
}
}
目标class是这样的:
public class TemplateLocationCalculator {
public String calculate(String string) {
return "index";
}
}
我说加载时间不会太长,希望您能同意我的看法。
潜力reason could be the component scanning and autowiring of components during start-up. You can limit this by creating a separate config
file for tests that limits the search space for components as explained here。
在配置中,您可以 lazy load beans <beans default-lazy-init="true">
or explicitly wire the beans (explained in more details here)
<beans ...>
<!-- this bean will be injected into the OrderServiceTest class -->
<bean id="target" class="TemplateLocationCalculator" />
<!-- other beans -->
</beans>
然后在测试中class,我们指定新的配置文件:
@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";
}
}
OP在这里。
经过 suggestion given in the chat I used the Microsoft Process Monitor 和 大量 过滤之后,我发现我的机器上 运行 正在使用 AV 软件 Avecto DefendPoint,并且它似乎这是瓶颈。每当我开始测试时,它都会 运行 大约 25%,这对我来说似乎表明它正在 运行 在我的四个内核之一的单个线程上全速运行。我不是这台机器的管理员,所以我无法禁用它来验证这个假设,但一般来说,如果其他人应该看到这个问题,请检查它是否可能是你的 anti-virus 软件。
我正在做一个相当小的项目(就依赖关系而言),每当我 运行 进行单元测试时,JVM 加载需要 8 秒,然后 运行 0.2秒实测
我的环境:
- Java 8
- Spring 工具套件 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("/"));
}
}
目标class是这样的:
public class TemplateLocationCalculator {
public String calculate(String string) {
return "index";
}
}
我说加载时间不会太长,希望您能同意我的看法。
潜力reason could be the component scanning and autowiring of components during start-up. You can limit this by creating a separate config
file for tests that limits the search space for components as explained here。
在配置中,您可以 lazy load beans <beans default-lazy-init="true">
or explicitly wire the beans (explained in more details here)
<beans ...>
<!-- this bean will be injected into the OrderServiceTest class -->
<bean id="target" class="TemplateLocationCalculator" />
<!-- other beans -->
</beans>
然后在测试中class,我们指定新的配置文件:
@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";
}
}
OP在这里。
经过 suggestion given in the chat I used the Microsoft Process Monitor 和 大量 过滤之后,我发现我的机器上 运行 正在使用 AV 软件 Avecto DefendPoint,并且它似乎这是瓶颈。每当我开始测试时,它都会 运行 大约 25%,这对我来说似乎表明它正在 运行 在我的四个内核之一的单个线程上全速运行。我不是这台机器的管理员,所以我无法禁用它来验证这个假设,但一般来说,如果其他人应该看到这个问题,请检查它是否可能是你的 anti-virus 软件。