1

I created an Embedded Sftp server bean for my integration tests, i hooked the startup and the shutdown of the server respectively with the afterPropertiesSet and destroy life cycles

public class EmbeddedSftpServer implements InitializingBean, DisposableBean {

    //other class content

    @Override
    public void afterPropertiesSet() throws Exception {
      //Code for starting server here
    }

    @Override
    public void destroy() throws Exception {
      //Code for stopping server here
    }
}

here my config class

@TestConfiguration
public class SftpTestConfig {
    @Bean
    public EmbeddedSftpServer embeddedSftpServer() {
        return new EmbeddedSftpServer();
    }

   //Other bean definitions
}

Now when i inject the bean in my test classes like the following :

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = SftpTestConfig .class)
class ExampleOneIT {
    @Autowired
    private EmbeddedSftpServer embeddedSftpServer;
}

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = SftpTestConfig .class)
class ExampleTwoIT {
    @Autowired
    private EmbeddedSftpServer embeddedSftpServer;
}

@SpringBatchTest
@ContextConfiguration(classes = SftpTestConfig .class)
class ExampleThreeIT {
    @Autowired
    private EmbeddedSftpServer embeddedSftpServer;
}

And i run all the test classes simultaneously, i found out that for the test classes annotated with @ExtendWith(SpringExtension.class), it's the same context that is used (which is understandable since i guess spring cache it) and therefore the bean lifecycle methods are not executed again, but to my surprise, for the class annotated with @SpringBatchTest i noticed that the life cycle hooks of the bean are executed again! Which is a behavior that is not convenient since i want the application context to start the server one time for all tests and close it at the end of those tests (which is the case if i use only @ExtendWith(SpringExtension.class) for all my test classes).

N.B. : I need to use @SpringBachTest for my ExampleThreeIT test class.

1 Answers1

0

I think you are hitting this issue: https://github.com/spring-projects/spring-batch/issues/3940 which has been fixed in v4.3.4/4.2.8. Upgrading to one of these versions should fix your issue.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50
  • Unfortunately, it doesn't fix the issue. Here a small projet which demonstrates the problem: https://github.com/ArchBp/spring-test-context-issue –  Nov 28 '21 at 08:28