Gangmax Blog

自由之思想,独立之精神

Spring Boot Unit Test Hang Issue

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
package demo.app;
2
3
import demo.app.DemoService;
4
import org.slf4j.Logger;
5
import org.slf4j.LoggerFactory;
6
import org.springframework.beans.factory.annotation.Autowired;
7
import org.springframework.boot.*;
8
import org.springframework.boot.autoconfigure.SpringBootApplication;
9
10
import java.util.Arrays;
11
12
@SpringBootApplication
13
public class Application implements ApplicationRunner, ExitCodeGenerator {
14
15
    private static Logger logger = LoggerFactory.getLogger(Application.class);
16
17
    @Autowired
18
    private DemoService demoServcie;
19
20
    public static void main(String[] args) {
21
        System.exit(SpringApplication.exit(SpringApplication.run(Application.class, args)));
22
    }
23
24
    @Override
25
    public void run(ApplicationArguments args) {
26
        logger.info("Starting...");
27
        logger.info("Application started with command-line arguments: {}", Arrays.toString(args.getSourceArgs()));
28
        logger.info("NonOptionArgs: {}", args.getNonOptionArgs());
29
        logger.info("OptionNames: {}", args.getOptionNames());
30
        for (String name : args.getOptionNames()) {
31
            logger.info("arg-" + name + "=" + args.getOptionValues(name));
32
        }
33
        this.demoService.execute();
34
        logger.info("Done.");
35
    }
36
37
    @Override
38
    public int getExitCode() {
39
        return 0;
40
    }
41
}

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

DemoTest.java
1
package demo.app;
2
3
import org.junit.jupiter.api.Test;
4
import org.junit.runner.RunWith;
5
import org.springframework.boot.test.context.SpringBootTest;
6
import org.springframework.test.context.junit4.SpringRunner;
7
8
@SpringBootTest
9
@RunWith(SpringRunner.class)
10
public class DemoTest {
11
    @Test
12
    public void test() {
13
        System.out.println("This is a demo.");
14
    }
15
}

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
package demo.app;
2
3
import demo.app.DemoService;
4
import org.slf4j.Logger;
5
import org.slf4j.LoggerFactory;
6
import org.springframework.beans.factory.annotation.Autowired;
7
import org.springframework.boot.*;
8
import org.springframework.boot.autoconfigure.SpringBootApplication;
9
10
import java.util.Arrays;
11
12
@SpringBootApplication
13
public class Application implements ApplicationRunner, ExitCodeGenerator {
14
15
    private static Logger logger = LoggerFactory.getLogger(Application.class);
16
17
    @Autowired
18
    private DemoService demoServcie;
19
20
    public static void main(String[] args) {
21
        System.exit(SpringApplication.exit(SpringApplication.run(Application.class, args)));
22
    }
23
24
    @Override
25
    public void run(ApplicationArguments args) {
26
        logger.info("Starting...");
27
        logger.info("Application started with command-line arguments: {}", Arrays.toString(args.getSourceArgs()));
28
        logger.info("NonOptionArgs: {}", args.getNonOptionArgs());
29
        logger.info("OptionNames: {}", args.getOptionNames());
30
        for (String name : args.getOptionNames()) {
31
            logger.info("arg-" + name + "=" + args.getOptionValues(name));
32
        }
33
        // Check if it's running inside of unit testing. Do the actual work only when it's not.
34
        if (!this.isRunningInTest()) {
35
            this.demoService.execute();
36
        }
37
        logger.info("Done.");
38
    }
39
40
    @Override
41
    public int getExitCode() {
42
        return 0;
43
    }
44
45
    private boolean isRunningInTest() {
46
        return "Test worker".equalsIgnoreCase(Thread.currentThread().getName());
47
    }
48
}

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