'FasterXML ObjectMapper is not working with ExecutorService in a Junit test

It is a very strange issue. Removing the JSON in TestUtil or the executorService/submit will make the following code working:

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

    public class ATest {
    
        @BeforeAll
        public static void setup(TestInfo test) throws Exception {
        }
    
        @Test
        void testThis(){
            int numThreads = 1;
            ExecutorService threadPool = Executors.newFixedThreadPool(numThreads);
            threadPool.submit(() -> {
                TestUtils.doSomething();
            });
        }
    }

Here is the class with the ObjectMapper>

import com.fasterxml.jackson.databind.ObjectMapper;

public class TestUtils {

    private static final ObjectMapper JSON;

    static {
        JSON = new ObjectMapper();
    }

    public static void doSomething() {
        System.out.println("entered the method");
    }
}

Currently, the method doSomething() would not be entered at all.



Solution 1:[1]

This issue will be resoved if we trigger the Junit test from Maven or if run it from a static main method.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Spider