Issue
Running Spring Boot 2.6.6 and Spring Cloud 2021.0.1
I'm attempting to migrate an existing service to Kubernetes so I added a dependency on spring-cloud-starter-kubernetes-client-all
. By default, I have spring.cloud.kubernetes.enable=false
and use the kubernetes
profile to enable it. This is intended to allow this service to operate in both Kubernetes and the legacy environment.
My unit-tests complete successfully when building locally but fail in my Bitbucket pipeline with the following error:
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.cloud.kubernetes.commons.config.NamespaceResolutionFailedException: unresolved namespace
I suspect this occurs because Bitbucket Pipelines are deployed in Kubernetes and Spring somehow detects this. I have tried the following to no avail
- Pass
--define SPRING_CLOUD_KUBERNETES_ENABLED=false
to Maven on the command line - Set this as an environment variable e.g.,
export SPRING_CLOUD_KUBERNETES_ENABLED=false
- Pass
--define spring.cloud.kubernetes.enabled=false
to Maven on the command line
I have also checked StackOverflow for similar issues and investigated the code also without avail. The class that is actually raising the issue is KubernetesClientConfigUtils
, which should be disabled.
I would appreciate any guidance you can provide.
Solution
After trying a few other approaches, I finally stumbled upon one that works. I added the property to the unit-test context using
@WebMvcTest(properties = { "spring.cloud.kubernetes.enabled=false" })
This should also work for any unit-test annotation that load a Spring application context such as @WebFluxTest
Answered By - Faron
Answer Checked By - Timothy Miller (JavaFixing Admin)