Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@SpringBatchTest with JUnit @Nested triggers new test context creation #4738

Open
scordio opened this issue Jan 3, 2025 · 0 comments · May be fixed by #4739
Open

@SpringBatchTest with JUnit @Nested triggers new test context creation #4738

scordio opened this issue Jan 3, 2025 · 0 comments · May be fixed by #4739
Labels
status: waiting-for-triage Issues that we did not analyse yet type: bug

Comments

@scordio
Copy link
Contributor

scordio commented Jan 3, 2025

Bug description
When using @SpringBatchTest on a test class enclosing a @Nested class, two separate Spring test contexts are created, one to execute the test methods in the outer class and another for the ones in the inner class.

Environment
Spring Batch 5.2.1 (originally detected on Spring Batch 4.3.10)

Steps to reproduce

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.batch.test.context.SpringBatchTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import static org.junit.jupiter.api.Assertions.assertSame;

@SpringJUnitConfig
@SpringBatchTest
class WithAnnotationTest {

  @Autowired
  ApplicationContext context;

  @Nested
  class Inner {

    @Autowired
    ApplicationContext context;

    @Test
    void test() {
      assertSame(WithAnnotationTest.this.context, context); // should succeed but fails
    }

  }

}

Expected behavior
A single test context should be available for both the outer and the inner class, like in the following example:

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import static org.junit.jupiter.api.Assertions.assertSame;

@SpringJUnitConfig
class WithoutAnnotationTest {

  @Autowired
  ApplicationContext context;

  @Nested
  class Inner {

    @Autowired
    ApplicationContext context;

    @Test
    void test() {
      assertSame(WithoutAnnotationTest.this.context, context); // succeeds
    }

  }

}

Workaround
Applying @SpringBatchTest also on the inner class prevents the creation of the additional test context:

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.batch.test.context.SpringBatchTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import static org.junit.jupiter.api.Assertions.assertSame;

@SpringJUnitConfig
@SpringBatchTest
class WithDoubleAnnotationTest {

  @Autowired
  ApplicationContext context;

  @Nested
  @SpringBatchTest
  class Inner {

    @Autowired
    ApplicationContext context;

    @Test
    void test() {
      assertSame(WithDoubleAnnotationTest.this.context, context); // succeeds
    }

  }

}

Minimal Complete Reproducible example
https://github.com/scordio/spring-boot-playground/tree/spring-batch-4738-reproducer

@scordio scordio added status: waiting-for-triage Issues that we did not analyse yet type: bug labels Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage Issues that we did not analyse yet type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant