I use spring 5 with junit 5 and I think that star/stop methods on application context must be called, because otherwise it is not possible to use context event listeners that can be very useful within testing infrastructure. So, I suggest to call .start() before ALL tests and .stop() after ALL tests.

Comment From: luoshuibing

hello,I don't think so

Comment From: sbrannen

So, I suggest to call .start() before ALL tests and .stop() after ALL tests.

I'm not quite sure what you mean by start() and stop(). Are you perhaps referring to the refresh() and close() methods in ConfigurableApplicationContext?

In any case, within the Spring TestContext Framework, refresh() is called by a SmartContextLoader when the ApplicationContext is created, and close() is called either via a JVM shutdown hook or when the ApplicationContext is evicted from the context cache (via @DirtiesContext or by automatic eviction due to the least recently used (LRU) eviction policy).

Please consult the reference manual for details.

In light of the above, I am closing this issue.

Comment From: PavelTurk

@sbrannen Unfortunately you didn't understand. So, please let me explain. There are two methods ConfigurableApplicationContext#start() and ConfigurableApplicationContext#stop(). When these methods are called we can get CONTEXT EVENTS in our listeners. These events can be very useful if we want to do some logic within testing infrastructure, for example to initialize/deinitialize some resources etc.

P.S. Please, don't close issues so quickly, it is a little bit impolite.

Comment From: ttddyy

Hi @PashaTurok

Since you want to perform some logic related to test infrastructure, I believe it is better to use feature that test framework provides.

For example, you can use "Test Execution Events", "TestExecutionListener", or even "TestContextBootStrapper". This way, your test infrastructure logic ties more on to testing lifecycle rather than application lifecycle.

Comment From: PavelTurk

@ttddyy Thank you for your suggestion, however I would like to have a single approach for production/test infrastructure that's why I paid attention to start() and stop() methods. I need ONLY ONE event when context begins its work and ONE ONE event when context finishes. I can agree that close() can be used for getting the second case, but I can't understand how refresh() can be used for the first case (re- for one event?!?!?). My experience with spring is poor but with all servers/containers I work start/stop were used for such purposes.

Comment From: ttddyy

@PashaTurok How are you calling application context's start() method in your main code? It seems starting application context(refresh()) doesn't call application context's start() on its own. (registered beans's start() are called but not for container) So, I'm guessing you are manually calling start() on the application context somewhere in your main application code. In that case, since it's done outside of what spring provides by default, I think you need to apply the start() call logic to the tests manually.

Comment From: PavelTurk

@ttddyy Yes, you are right. In main code I call start() manually as soon as I create context. However, when sprint-test is used, context is created inside it. That's why I suggest to call these methods there and this is the reason of this issue.