Skip to content

Bean destroyMethod is not being called. #4208

@gandola

Description

@gandola

Hi,

I'm trying to update to spring boot 1.2.6 and I'm having an issue with the destroyMethod that I was not having on previous versions (1.2.3 to 1.2.5).

I'm creating a simple embedded RedisServer bean available only for testing and I'm using the destroyMethod to stop the server after all tests run. This was working fine with previous versions but with 1.2.6 version the destroyMethod is not being called.

Small project reproducing this issue:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>tests</groupId>
    <artifactId>test-destroy</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <spring.version>1.2.5.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${spring.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Main class

package app;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication

@Configuration
@ComponentScan("app")
public class Main {
    public static void main(String[] args) {
        new SpringApplicationBuilder()
                .sources(Main.class)
                .web(false)
                .run(args);
    }
}

On the test folder

package app;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ServiceTestConfiguration {

    public class Service {

        public void start() {
            System.out.println("Started!!!");
        }

        public void stop() {
            System.out.println("Stopped!!!");
        }

        public void test() {
            System.out.println("Test!!!");
        }
    }

    @Bean(destroyMethod = "stop")
    public Service createBean() {
        Service destroyTest = new Service();
        destroyTest.start();
        return destroyTest;
    }
}
package app;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Main.class)
public class ServiceTest {

    @Autowired
    private ServiceTestConfiguration.Service test;

    @Test
    public void test() {
        test.test();
    }
}

With < 1.2.6 it prints:

Started!!!
Test!!!
Stopped!!!

With 1.2.6 it prints:

Started!!!
Test!!!

Could this be an issue with the spring-boot-starter-test lib?

Thank you,
Cheers

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions