【问题标题】:Spring: @PostConstruct is not called on maven tests, but works fine if test is running in IDEASpring:在 Maven 测试中不调用 @PostConstruct,但如果测试在 IDEA 中运行,则可以正常工作
【发布时间】:2018-08-04 10:55:54
【问题描述】:

问题是 @PostConstruct 方法在 maven 测试期间没有被调用,但如果我在 IDEA 中运行这些测试,它可以正常工作。

为什么在 maven 测试期间没有调用它,我该如何解决这个问题?

我的测试课:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AppConfig.class, DBConfig.class})
@WebAppConfiguration
public class SecurityTest {

    @Autowired
    private WebApplicationContext context;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        mockMvc = MockMvcBuilders
                .webAppContextSetup(context)
                .apply(springSecurity())
                .build();
    }

    @Test
    public void openLibraryPage() throws Exception {
        mockMvc.perform(get("/books")
                .with(httpBasic("admin", "admin")))
                .andDo(print())
                .andExpect(status().is2xxSuccessful());
    }

}

包含@PostConstruct的类(这个配置工作正常,这个类中的bean在maven测试和idea测试中都被初始化了,只是在maven测试中没有调用@PostConstruct方法。原因)

@Configuration
@ComponentScan("test.task")
public class DBConfig {

    private static final Logger log = LogManager.getLogger(DBConfig.class);

    @Autowired
    private DataSource dataSource;

    @PostConstruct
    public void initializeDatabase() {
        .. some code ..
    }

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource("jdbc:h2:mem:test-task;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE", "sa", "");
        dataSource.setDriverClassName("org.h2.Driver");
        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(dataSource);
    }

}

还有我的 pom.xml

<?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>test.task</groupId>
    <artifactId>library</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>default-war</id>
                        <phase>prepare-package</phase>
                        <configuration>
                            <failOnMissingWebXml>false</failOnMissingWebXml>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.197</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>5.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>5.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <version>5.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.11.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.11.1</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
            <version>3.0.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
            <version>3.6.2</version>
        </dependency>
    </dependencies>

</project>

# 更新 1

DBConfig.class 添加到@ContextConfiguration,没有任何变化。

【问题讨论】:

    标签: java spring maven junit


    【解决方案1】:

    遇到同样的问题,对我来说这是因为以下依赖项不在类路径上:

    <!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
    <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.3.2</version>
    </dependency>
    

    IntelliJ 和 Maven 是否使用相同的 Java 版本?也许 IntelliJ 使用的是 Java = 9,因此需要我提到的依赖项。

    See this answer

    【讨论】:

    • 感谢您的回答。如果您在另一个 StackOverflow 帖子中找到答案,可以将其标记为“重复”。
    • 版本相同(java 8),但无论如何这都有帮助
    【解决方案2】:

    开始测试时,您只是将AppConfig 加载到上下文中。而且,似乎DBConfig 没有在上下文中加载,这就是initializeDatabase 方法没有被调用的原因。

    要使其工作,必须加载DBConfig

    @ContextConfiguration(classes = {AppConfig.class, DBConfig.class})

    【讨论】:

    • 我试过了,但没有任何改变。此外,DBConfig 中的 bean 似乎在没有将 DBConfig.class 添加到 @ContextConfiguration 的情况下初始化良好
    • @SpringBootTest(classes = {AppConfig.class, DBConfig.class})。你能试试这个吗?
    • 我确实尝试过,但没有任何改变
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 2019-04-11
    • 2021-08-13
    相关资源
    最近更新 更多