8

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:

  • Java 8
  • Spring Tool Suite 3.8.1.RELEASE
  • JUnit 4
  • Windows 8

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.

Lii
  • 11,553
  • 8
  • 64
  • 88
kinbiko
  • 2,066
  • 1
  • 28
  • 40
  • 1
    @PieterDeBie I did. Second paragraph. Computer hardware performance shouldn't be a problem. – kinbiko Jan 18 '17 at 14:01
  • Read too fast, deleted my comment :) – Pieter De Bie Jan 18 '17 at 14:04
  • 1
    Can you profile what happens during these 8 seconds of start-up? You may try an approach described in [this question](http://stackoverflow.com/questions/39321345/how-do-i-measure-jvm-startup-time) to log various JVM bootstrap events. – apangin Jan 18 '17 at 22:37
  • @apangin Thanks for the suggestion. I tried it, but the logging doesn't start until the application (instead of the JVM) starts. – kinbiko Jan 23 '17 at 12:56
  • @NicolasFilotto I'm using STS (essentially Eclipse + Spring plugins), with the version mentioned in the question. – kinbiko Jan 23 '17 at 14:11
  • could you show us the classpath you are using to run your test? – Roland Jan 23 '17 at 14:30
  • @vikingsteve you are seeing all of my code there. I want to run a simple unit test. I just happen to be running STS. – kinbiko Jan 23 '17 at 14:30
  • Im seeing your code for the test but I'm not seeing the full spring application context. – vikingsteve Jan 23 '17 at 14:33
  • @Roland I'd be happy to. Unfortunately, I'm not entirely sure how to go about showing you that. The `Run As > Run configurations...` window in eclipse only show my JRE, resources folder, my project and maven dependencies. Unfortunately, it doesn't go into any more detail than that...? – kinbiko Jan 23 '17 at 14:33
  • What happens if you go externally to your ide (on the command line) and run `mvn test` ? – vikingsteve Jan 23 '17 at 14:34
  • @vikingsteve that would be because I'm not using Spring... – kinbiko Jan 23 '17 at 14:35
  • 1
    sorry i misread spring tool suite as spring. Ok then, you're using Eclipse IDE? What if you open your test in IntelliJ, does it run faster there? – vikingsteve Jan 23 '17 at 14:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/133816/discussion-between-kinbiko-and-vikingsteve). – kinbiko Jan 23 '17 at 14:52
  • 1. Are you running the tests or are you debugging the tests? 2. What happens if you run `mvn test` ? – Mike Nakis Jan 26 '17 at 09:10
  • How do you expect to receive any answers if you are not answering our questions? – Mike Nakis Jan 29 '17 at 00:52
  • @MikeNakis I appreciate your efforts to help me with this. Both of your questions have been answered previously, either above or in chat. Just like the comment you deleted earlier asking which IDE I was using. But just to be abundantly clear: 1. I am *RUNNING* the tests. The word `debug` has not been used in this question until you mentioned it. 2. See chat, Mon 14:58. – kinbiko Jan 29 '17 at 01:57

2 Answers2

3

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.

Community
  • 1
  • 1
kinbiko
  • 2,066
  • 1
  • 28
  • 40
-1

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";
   }

}
Community
  • 1
  • 1
Aimee Borda
  • 842
  • 2
  • 11
  • 22
  • 1
    Spring is not in use here. It's the Spring Tool Suite (STS). Or did you mean that STS is autowiring under the hood even if a plain and simple JUnit-test is run? – Roland Jan 30 '17 at 07:04
  • STS is a plugin that pre-installs Spring IDE components - under the hood it is still spring. Even though it is just a JUnit test, the application needs to be build and run so the auto wiring still takes place. The idea here is to simplify this process – Aimee Borda Jan 30 '17 at 07:24
  • ok... I meant that Spring is not directly in use here. If STS is doing some wiring under the hood which slows down the process and your answer solves that, everything is fine ;-) – Roland Jan 30 '17 at 07:28