'Adding @SpringBootTest to test class hang unit tests

I want to test my Spring application. When I add the @SpringBootTest to my test class, tests hang and not starting even if I wait for more than an hour! Removing my SpringBootTest annotation results failure in initializing @Value fields and I can not test any of my component classes. My Configuration class code:

@ConditionalOnProperty(value = "app.scheduling.enable", havingValue = "true", matchIfMissing = true)
@Configuration
@EnableScheduling
public class Configurer {    
private static ApplicationContext context = new AnnotationConfigApplicationContext("ir.isc.nps.ach.pasha");
    public static ApplicationContext getContext() {
        return context;
    }
}

My Test class:

@SpringBootTest
@ContextConfiguration(classes = Configurer.class)
@TestPropertySource("classpath:application.properties")
public class ConfigurerTest {
    @Test
    void getContext() {
        assertNotNull(Configurer.getContext());
    }
}

stack trace while running test class:

12:15:41.488 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@1a18644 testClass = PashaApplicationTests, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@5acf93bb testClass = PashaApplicationTests, locations = '{}', classes = '{class ir.isc.nps.ach.pasha.Configurer}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{classpath:application.properties}', propertySourceProperties = '{jobs.enable.scheduler=true, cron.expression=* * * ? * *, app.scheduling.enable=true, thread.poolCount=32, inputFile.dir=/TestResources, acceptedFolder.dir=\accepted, errorFolder.dir=\error, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@7e0b85f9, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@6a28ffa4, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@72f926e6, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@7714e963], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true]], class annotated with @DirtiesContext [false] with mode [null].
12:15:41.567 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, jobs.enable.scheduler=true, cron.expression=* * * ? * *, app.scheduling.enable=true, thread.poolCount=32, inputFile.dir=/TestResources, acceptedFolder.dir=\accepted, errorFolder.dir=\error, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=-1}
2020-03-16 12:15:41 INFO  i.i.n.a.Tests - Starting Tests on  with PID (started by  in )
2020-03-16 12:15:41 INFO  i.i.n.a.Tests - No active profile set, falling back to default profiles: default
2020-03-16 12:15:43 INFO  o.s.d.r.c.RepositoryConfigurationDelegate - Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-03-16 12:15:43 INFO  o.s.d.r.c.RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 94ms. Found 6 JPA repository interfaces.

The test is hanging when I run it:

enter image description here



Solution 1:[1]

This error sometimes occurred, when there is a static method likely a method annotated by @BeforeAll or @AfterAll thus a field(s) of unit test class is called in the static method defined static and annotated by @Autowired.
You should use either @Autowired or key word static not both, because the field annotated by @Autowired can not be static.

Solution 2:[2]

@SpringBootTest can't load properties server.port

discover whether to use ${server.port}, replace with a specific port

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 Shadyar
Solution 2 fatCop