<p>Running Spring Boot 2.6.6 and Spring Cloud 2021.0.1</p>
<p>I'm attempting to migrate an existing service to Kubernetes so I added a dependency on <code>spring-cloud-starter-kubernetes-client-all</code>. By default, I have <code>spring.cloud.kubernetes.enable=false</code> and use the <code>kubernetes</code> profile to enable it. This is intended to allow this service to operate in both Kubernetes and the legacy environment.</p>
<p>My unit-tests complete successfully when building locally but fail in my Bitbucket pipeline with the following error:</p>
<pre><code>java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.cloud.kubernetes.commons.config.NamespaceResolutionFailedException: unresolved namespace
</code></pre>
<p>I suspect this occurs because Bitbucket Pipelines are deployed in Kubernetes and Spring somehow detects this. I have tried the following to no avail</p>
<ul>
<li>Pass <code>--define SPRING_CLOUD_KUBERNETES_ENABLED=false</code> to Maven on the command line</li>
<li>Set this as an environment variable e.g., <code>export SPRING_CLOUD_KUBERNETES_ENABLED=false</code></li>
<li>Pass <code>--define spring.cloud.kubernetes.enabled=false</code> to Maven on the command line</li>
</ul>
<p>I have also checked StackOverflow for similar issues and investigated the code also without avail. The class that is actually raising the issue is <code>KubernetesClientConfigUtils</code>, which should be disabled.</p>
<p>I would appreciate any guidance you can provide.</p>

<p>Spring Cloud checks whether the application is running in a K8s environment before loading the active spring profile configuration and adds <code>kubernetes</code> to the active profiles. Previously, in Hoxton SR10, the profile was identified and <code>bootstrap-&lt;profile&gt;.yml</code> loaded before checking for Kubernetes. <code>spring.cloud.kubernetes.enabled</code> was picked up from there if set in the profile configuration or the maven pom properties.</p>
<p>As maven allows setting system properties on the command line, kubernetes detection can be disabled by setting it there:</p>
<pre><code>mvn test -Dspring.cloud.kubernetes.enabled=false
</code></pre>
<p>The surefire maven plugin allows setting system properties for all tests, so it's possible to set <code>spring.cloud.kubernetes.enabled</code> to be <code>false</code> in the surefire plugin configuration.</p>
<pre><code>&lt;plugin&gt;
    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
    &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
    &lt;configuration&gt;
        &lt;systemPropertyVariables&gt;
            &lt;spring.cloud.kubernetes.enabled&gt;false&lt;/spring.cloud.kubernetes.enabled&gt;
        &lt;/systemPropertyVariables&gt;
    &lt;/configuration&gt;
&lt;/plugin&gt;
</code></pre>
<p>It is also possible to set the configuration on individual test classes using @Faron's approach to explicitly set the property in any <code>WebMvcTest</code> annotated unit test, e.g.:</p>
<pre><code>@WebMvcTest(properties = { &quot;spring.cloud.kubernetes.enabled=false&quot; })
</code></pre>
<p>It should also work on other unit test annotation that loads a Spring application context, such as <code>WebFluxTest</code>.</p>
