October 26, 2024
Chicago 12, Melborne City, USA
java

ApplicationContext resulting in java.lang.IllegalStateException


this is my application, where I’m trying to run some test cases. I’ve provided the stack trace and then the sample code. Let me know what do you all think

Test Class

package com.list.loader.controller;

import io.restassured.http.ContentType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.containsString;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(locations="classpath:application-test.properties")
public class ListLoaderInfoControllerIT {

    @LocalServerPort
    private int port;

    @Test
    public void should_get_running_environment() {
        String path = "/";
        given()
                .port(port)
                .contentType(ContentType.JSON)
                .when()
                .get(path )
                .then()
                .statusCode(HttpStatus.OK.value())
                .body(containsString("List Loader"));
    }
}

Java error stack trace

The ApplicationContext loaded for [[WebMergedContextConfiguration@4f069f13 testClass = ListLoaderInfoControllerIT, locations="{}", classes="{class com.list.loader.Application}", contextInitializerClasses="[]", activeProfiles="{}", propertySourceLocations="{classpath:application-test.properties}", propertySourceProperties="{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=0}", contextCustomizers = set[org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@217196d8, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@a14a72b, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@3897abb6, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@6166c50, org.springframework.boot.test.context.SpringBootTestArgs@1], resourceBasePath="src/main/webapp", contextLoader="org.springframework.boot.test.context.SpringBootContextLoader", parent = [null]]] is not active. This may be due to one of the following reasons: 1) the context was closed programmatically by user code; 2) the context was closed during parallel test execution either according to @DirtiesContext semantics or due to automatic eviction from the ContextCache due to a maximum cache size policy.
java.lang.IllegalStateException: The ApplicationContext loaded for [[WebMergedContextConfiguration@4f069f13 testClass = ListLoaderInfoControllerIT, locations="{}", classes="{class com.list.loader.Application}", contextInitializerClasses="[]", activeProfiles="{}", propertySourceLocations="{classpath:application-test.properties}", propertySourceProperties="{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=0}", contextCustomizers = set[org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@217196d8, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@a14a72b, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@3897abb6, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@6166c50, org.springframework.boot.test.context.SpringBootTestArgs@1], resourceBasePath="src/main/webapp", contextLoader="org.springframework.boot.test.context.SpringBootContextLoader", parent = [null]]] is not active. This may be due to one of the following reasons: 1) the context was closed programmatically by user code; 2) the context was closed during parallel test execution either according to @DirtiesContext semantics or due to automatic eviction from the ContextCache due to a maximum cache size policy.
    at org.springframework.util.Assert.state(Assert.java:94)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:127)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:43)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:110)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)
    at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:62)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
    at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
    at com.sun.proxy.$Proxy5.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:119)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:182)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:164)
    at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:414)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
    at java.base/java.lang.Thread.run(Thread.java:829)

Test Properties

# Main DB (Target DB) Postgresql
spring.db2.url=${DB_1_URL}
spring.test.parallel.enabled=false
spring.test.context.cache.size=20

spring.db1.username=postgres
spring.db1.password=root
spring.db1.driver-class-name=org.postgresql.Driver
spring.db1.testOnBorrow=true
spring.db1.validationQuery="select 1"
spring.db1.autoCommit=false

spring.db2.url=Constants.URL
spring.db2.username=Constants.ID
spring.db2.password=Constants.ID
spring.db2.driver-class-name=com.ibm.as400.access.AS400JDBCDriver
spring.db2.testOnBorrow=true
spring.db2.validationQuery="VALUES current date"


# APP Logging
logging.level.org.springframework.web=DEBUG

#  SQL LOGGING
#logging.level.org.springframework.jdbc.core=DEBUG

#logging.level.org.hibernate.SQL=DEBUG
#logging.level.org.hibernate.type.descriptor.sql.BasicBinder=INFO

logging.file=logs/app.log

#Disable Banner
spring.main.banner-mode=off

#schedule
loader.cron=0 0 1 * * ?
loader.zone=CST
loader.migrateUserMapping=false
loader.batch.id=10
loader.batch.skipCount=1

spring.batch.job.enabled=false
management.endpoints.web.exposure.include=*

spring.main.allow-bean-definition-overriding=true
env=qa

I would have to point out that which ever fields that I populate from the environmental variables have all bee fulfilled. There were a little more of those env variables in the running envs for this test case. I’m quite certain that there is just something very minimal that I’m missing, would be grateful to know.

I tried hard coding all the necessary values to the properties file. I got to know that the application context closes beforehand, but I am unable to find occurrences where it gets closed, or so I believe. There I’ve added cache size and then disabled parallel running of tests. Still the same.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video