Reputation: 2155
I'm working on a fairly small project (in terms of dependencies), and whenever I run a unit test it takes 8 seconds for the JVM to load, before running the actual test in 0.2s.
My environment:
I fear there must be something in my environment that's causing this to take so long, and I'm hoping someone has seen this before and found the source of the problem and perhaps a solution?
E.g. if my PATH
environment variable is really long, would that matter at all?
What exactly happens when I run a JUnit test?
The actual test I'm trying to run is:
public class TemplateLocationCalculatorTest {
private TemplateLocationCalculator target = new TemplateLocationCalculator();
@Test
public void whenGivenRootReturnIndex(){
Assert.assertEquals("index", target.calculate("/"));
}
}
And the target class is this:
public class TemplateLocationCalculator {
public String calculate(String string) {
return "index";
}
}
I hope you'll agree with me when I say this shouldn't take long to load.
Upvotes: 8
Views: 4509
Reputation: 2155
OP here.
Following a suggestion given in the chat I used the Microsoft Process Monitor and after a lot of filtering I discovered that the AV software Avecto DefendPoint was running on my machine, and that it seemed like this was the bottleneck. Whenever I start a test, it would run at about 25%, which to me seems to indicate that it's running at full speed on a single thread on one of my four cores. I'm not the administrator on this machine so I was unable to disable it to verify this hypothesis, but in general if other people should see this issue check if it could be your anti-virus software.
Upvotes: 3
Reputation: 842
A potential 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.
In the config, you can either 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>
Then in the tests class, we specify the new config file:
@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";
}
}
Upvotes: -1