Gangmax Blog

自由之思想,独立之精神

Spring Boot Unit Test Hang Issue

| Comments

When I’m writting an Spring Boot command line application and I find this issue. Here is how I solve it.

The issue is, after I add actual logic in the “Application.java” file, which is the entrance of the appliction, running unit tests will always hang. Here is the source code of “Application.java”:

Application.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package demo.app;

import demo.app.DemoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Arrays;

@SpringBootApplication
public class Application implements ApplicationRunner, ExitCodeGenerator {

    private static Logger logger = LoggerFactory.getLogger(Application.class);

    @Autowired
    private DemoService demoServcie;

    public static void main(String[] args) {
        System.exit(SpringApplication.exit(SpringApplication.run(Application.class, args)));
    }

    @Override
    public void run(ApplicationArguments args) {
        logger.info("Starting...");
        logger.info("Application started with command-line arguments: {}", Arrays.toString(args.getSourceArgs()));
        logger.info("NonOptionArgs: {}", args.getNonOptionArgs());
        logger.info("OptionNames: {}", args.getOptionNames());
        for (String name : args.getOptionNames()) {
            logger.info("arg-" + name + "=" + args.getOptionValues(name));
        }
        this.demoService.execute();
        logger.info("Done.");
    }

    @Override
    public int getExitCode() {
        return 0;
    }
}

The unit test is just plain Spring Boot testing code like below:

DemoTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package demo.app;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class DemoTest {
    @Test
    public void test() {
        System.out.println("This is a demo.");
    }
}

Note that even if the unit test does nothing meaningful, it will still hang when running. I also find that, if I comment out the “this.demoService.execute()” line, the unit test can run without problem. So I update the “Application.java”:

Application.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package demo.app;

import demo.app.DemoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Arrays;

@SpringBootApplication
public class Application implements ApplicationRunner, ExitCodeGenerator {

    private static Logger logger = LoggerFactory.getLogger(Application.class);

    @Autowired
    private DemoService demoServcie;

    public static void main(String[] args) {
        System.exit(SpringApplication.exit(SpringApplication.run(Application.class, args)));
    }

    @Override
    public void run(ApplicationArguments args) {
        logger.info("Starting...");
        logger.info("Application started with command-line arguments: {}", Arrays.toString(args.getSourceArgs()));
        logger.info("NonOptionArgs: {}", args.getNonOptionArgs());
        logger.info("OptionNames: {}", args.getOptionNames());
        for (String name : args.getOptionNames()) {
            logger.info("arg-" + name + "=" + args.getOptionValues(name));
        }
        // Check if it's running inside of unit testing. Do the actual work only when it's not.
        if (!this.isRunningInTest()) {
            this.demoService.execute();
        }
        logger.info("Done.");
    }

    @Override
    public int getExitCode() {
        return 0;
    }

    private boolean isRunningInTest() {
        return "Test worker".equalsIgnoreCase(Thread.currentThread().getName());
    }
}

With this update, this issue is solved. However, I’m not sure the root cause of this issue: does it mean the unit test execution has to start after the “Application.class” returns?

Comments