gpt4 book ai didi

java - Spring:@PostConstruct 在 Maven 测试中不会被调用,但如果测试在 IDEA 中运行则工作正常

转载 作者:行者123 更新时间:2023-11-30 02:04:45 24 4
gpt4 key购买 nike

问题是 @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测试中都被初始化,只是@PostConstruct方法没有被初始化)由于某种原因在 Maven 测试中调用)

@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,没有任何改变。

最佳答案

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

<!-- 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,这就是它工作的原因,而 Maven 使用 Java >= 9,因此需要我提到的依赖项。

See this answer

关于java - Spring:@PostConstruct 在 Maven 测试中不会被调用,但如果测试在 IDEA 中运行则工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51684872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com