[英]Spring: @PostConstruct is not called on maven tests, but works fine if test is running in IDEA
The problem is that the @PostConstruct
method is not invoked during maven tests, but works fine if I run these tests in IDEA. 问题在于,在Maven测试期间不会调用
@PostConstruct
方法,但是如果我在IDEA中运行这些测试,则可以正常工作。
Why is it not invoked during maven tests and how do I fix this? 为什么在Maven测试期间未调用它,我该如何解决?
My test class: 我的测试课:
@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());
}
}
The class, that contains @PostConstruct
(this configuration works fine and beans in this class are initialized both in maven tests and in idea tests, it's just the @PostConstruct method is not being invoked in maven tests for some reason) 包含
@PostConstruct
的类(此配置工作正常,并且该类中的bean在maven测试和思想测试中均被初始化,只是由于某种原因未在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);
}
}
and my pom.xml 和我的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>
# UPDATE 1 #更新1
Added DBConfig.class
to @ContextConfiguration
, nothing has changed. 新增
DBConfig.class
到@ContextConfiguration
,一切都没有改变。
While starting the test, you are just loading the AppConfig
into the context. 开始测试时,您只是将
AppConfig
加载到上下文中。 And, it seems like DBConfig
is not loaded in the context, which is the reason for initializeDatabase
method not getting invoked. 而且,似乎
DBConfig
没有加载到上下文中,这就是未调用initializeDatabase
方法的原因。
To make it work, DBConfig
has to be loaded, 为了使其正常工作,必须加载
DBConfig
,
@ContextConfiguration(classes = {AppConfig.class, DBConfig.class})
@ContextConfiguration(classes = {AppConfig.class,DBConfig.class})
Had the same problem and for me it was because the following dependency was not on the classpath: 有同样的问题,对我来说是因为以下依赖项不在classpath上:
<!-- 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>
Are IntelliJ and Maven using the same Java Version? IntelliJ和Maven是否使用相同的Java版本? Maybe IntelliJ is using Java < 9, that's why it works and Maven uses Java >= 9 and therefore needs the dependency i mentioned.
也许IntelliJ使用Java <9,这就是为什么它可以工作,而Maven使用Java> = 9,因此需要我提到的依赖项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.