<p>I have a simple integration test for my spring boot application under the location <code>src/intrgTest/groovy</code> and below is the test</p>
<pre><code>@AutoConfigureMockMvc
@WebMvcTest
class WebControllerTest extends Specification {

  @Autowired
  private MockMvc mvc

  def &quot;when get is performed then the response has status 200 and content is 'Hello world!'&quot;() {
    expect: &quot;Status is 200 and the response is 'Hello world!'&quot;
    mvc.perform(get(&quot;/hello&quot;))
      .andExpect(status().isOk())
      .andReturn()
      .response
      .contentAsString == &quot;Hello world!&quot;
  }
}
</code></pre>
<p>When I create a pod in Kubernetes I want to run this test case to check the app is working fine. How can I achieve this?</p>
<p>Below is the deployment.yml file for kubernetes</p>
<pre><code>apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: spring-boot-deployment
spec:
  selector:
    matchLabels:
      app: spring-boot-app
  replicas: 2 
  template:
    metadata:
      labels:
        app: spring-boot-app
    spec:
      containers:
      - name: spring-boot-app
        image: spring-boot-test-images:63
        ports:
        - containerPort: 80
</code></pre>

<p>Looking at the test it's a health check rather than an integration test. Ideally integration tests should be run using <code>mvn test</code> as part of Continuous Integration before you perform the actual deployment and not after that.</p>
<p>You really don't need to write a test for health check and execute it after the application is deployed on kubernetes. You could simply define <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/" rel="nofollow noreferrer">readiness probe</a> in the deployment yaml and Kubernetes will perform a health check before marking the pod as READY and start to send traffic to it.</p>
<p>If you are using spring boot version older than 2.3 then  you can make use of actuator endpoint <code>/actuator/health</code> and if you are using spring boot 2.3 then <code>/actuator/health/readiness</code> endpoint as readiness probe.</p>
<pre><code>apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: spring-boot-deployment
spec:
  selector:
    matchLabels:
      app: spring-boot-app
  replicas: 2 
  template:
    metadata:
      labels:
        app: spring-boot-app
    spec:
      containers:
      - name: spring-boot-app
        image: spring-boot-test-images:63
        ports:
        - containerPort: 80
        readinessProbe:
          httpGet:
            port: 8080
            path: /actuator/health
        initialDelaySeconds: 10
</code></pre>
<p>If you want to include tests on some external systems such as redis or dynamo as health check you could write a <a href="https://docs.spring.io/spring-boot/docs/2.3.0.BUILD-SNAPSHOT/reference/html/production-ready-features.html#production-ready-kubernetes-probes-external-state" rel="nofollow noreferrer">custom health indicator</a> for that. Below example from spring provided <code>RedisHealthIndicator</code></p>
<pre><code>public class RedisHealthIndicator extends AbstractHealthIndicator {

    private final RedisConnectionFactory redisConnectionFactory;

    public RedisHealthIndicator(RedisConnectionFactory connectionFactory) {
        super(&quot;Redis health check failed&quot;);
        Assert.notNull(connectionFactory, &quot;ConnectionFactory must not be null&quot;);
        this.redisConnectionFactory = connectionFactory;
    }

    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        RedisConnection connection = RedisConnectionUtils.getConnection(this.redisConnectionFactory);
        try {
            doHealthCheck(builder, connection);
        }
        finally {
            RedisConnectionUtils.releaseConnection(connection, this.redisConnectionFactory, false);
        }
    }

    private void doHealthCheck(Health.Builder builder, RedisConnection connection) {
        if (connection instanceof RedisClusterConnection) {
            RedisHealth.up(builder, ((RedisClusterConnection) connection).clusterGetClusterInfo());
        }
        else {
            RedisHealth.up(builder, connection.info());
        }
    }

}
</code></pre>
