我有一个带有一个测试类 DbTest 的Maven模块。运行测试时,不会拾取属性文件application.properties。线 DvsTestDbConfig 中的 System.out.println 显示{database.test}而不是qqqqqqqqq(当前的虚拟值)。知道为什么spring找不到属性文件吗?
DbTest
package ch.admin.estv.dvs.test.database;
import ch.admin.estv.dvs.test.database.config.DvsTestDbConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@Import(DvsTestDbConfig.class)
public class DbTest {
@Test
public void givenPartnerToDelete_whenPartnerDelete_thenNoEntryExist() {
System.out.println("test started");
}
}
DvsTestDbConfig
package ch.admin.estv.dvs.test.database.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@ComponentScan(basePackages = {"ch.admin.estv.dvs.test.database"})
@EnableTransactionManagement
public class DvsTestDbConfig {
@Value("${database.test}")
String databaseTest;
@Autowired
private Environment environment;
@Value("${database.driverClassName}")
String driverClassName;
@Value("${database.url}")
String databaseUrl;
@Value("${database.username}")
String databaseUsername;
@Value("${database.password}")
String databasePassword;
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[]{"ch.admin.estv.dvs.test.database"});
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
System.out.println("************** db property databaseTest : " + databaseTest);
dataSource.setUrl(databaseUrl);
dataSource.setUsername(databaseUsername);
dataSource.setPassword(databasePassword);
dataSource.setDriverClassName("oracle.jdbc.OracleDriver");
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
Properties additionalProperties() {
Properties properties = new Properties();
//properties.setProperty("hibernate.hbm2ddl.auto", "validate");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
properties.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false");
properties.setProperty("hibernate.jdbc.lob.non_contextual_creation", "true");
properties.setProperty("show_sql", "true");
return properties;
}
}
application.properties
database.test=qqqqqqqqq
database.url=xxxxxxxxxxxxxxxxx
database.username=xxxxxxxxxx
database.password=xxxxxxxxxxx
database.driverClassName=oracle.jdbc.OracleDriver
pom.xml
<parent>
<artifactId>dvs-test</artifactId>
<groupId>ch.admin.estv.dvs</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dvs-test-database</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>javax.transaction-api</artifactId>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>${ojdbc6.version}</version>
</dependency>
</dependencies>
答案 0 :(得分:0)
用@SpringBootTest
注释测试类。更多详细信息和替代方法:Populating Spring @Value during Unit Test
答案 1 :(得分:0)
添加
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="@android:color/holo_blue_light"
android:elevation="4dp"
android:paddingTop="7dp"
app:layout_scrollFlags="scroll|enterAlways"
app:titleTextColor="@android:color/white">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="center_horizontal|top"
android:gravity="center"
android:text="@string/toolbar_title_placeholder"
android:textColor="@android:color/white"
android:textSize="20sp" />
</androidx.appcompat.widget.Toolbar>
DpsTestDbConfig类上的注释
通过环境解析的属性位于一个或多个中 “属性源”对象和@Configuration类可能会有所贡献 属性资源使用@PropertySource传递给Environment对象 注释: