Spring测试未从application.properties填充数据库配置

在我的spring boot应用程序中,我有以下调用存储过程的类

public class FmTrfUtil {

static int returnVal;

public static int insertFmTrfs(List<String> trfs, String source) {

EntityManager em = Persistence.createEntityManagerFactory("RIStore_FM").createEntityManager();

Session session = em.unwrap( Session.class );

final String[] trfArray = trfs.toArray(new String[trfs.size()]);

final String src = source;

session.doWork( new Work(){

public void execute(Connection conn) throws SQLException {

CallableStatement stmt = null;

OracleConnection oraCon = conn.unwrap(OracleConnection.class);

Array array = oraCon.createARRAY("VARCHAR2_TAB_T", trfArray);

stmt = conn.prepareCall("{? = call FM_TRF_UTIL.process_fm_trf(?,?)}");

stmt.registerOutParameter(1, Types.INTEGER);

stmt.setArray(2, array);

stmt.setString(3, src);

stmt.execute();

returnVal = stmt.getInt(1);

}

});

return returnVal;

}

}

现在,我想使用Spring Test 集成测试Spring Boot

Application中的示例,在该类上运行集成测试。

@RunWith(SpringJUnit4ClassRunner.class)

@SpringApplicationConfiguration(classes = RistoreWebApplication.class)

@WebAppConfiguration

public class FmTrfUtilTest {

static EntityManagerFactory factory = null;

static EntityManager manager = null;

@Test

public void test() {

List<String> trfs = new ArrayList<String>();

trfs.add("TRF000001");

trfs.add("TRF000002");

int ret = FmTrfUtil.insertFmTrfs(trfs, "SARC");

assertTrue(ret > 0);

}

}

RistoreWebApplication是主应用程序类的名称,它是api的入口点。根据该网页,“

@SpringApplicationConfiguration注释将触发用于读取特定于Spring Boot的配置,属性等的逻辑。”

我的理解是,这还将在我的application.properties中加载数据库连接信息:

spring.datasource.url=jdbc:oracle:thin:@ldap://xxx:389/risdev3, cn=OracleContext,dc=yyy,dc=com

spring.datasource.username=rowner

spring.datasource.password=rowner987

spring.datasource.driverClassName=oracle.jdbc.OracleDriver

但是,当我运行此测试时,我得到了UnsupportedOperationException: The application must supply

JDBC connections。我如何准确地加载运行测试的数据库连接配置?我正在使用Spring Boot 1.3

因为application.properties位于src / main / resources中,而我的测试类位于src / test

/ java中,所以我认为src/test/resources基于SpringjUnitTesting属性文件的答案,我还会有另一个属性文件。因此,我这样做了,并@PropertySource("classpath:application-test.properties")在测试类中添加了注释,但是仍然遇到相同的错误。有没有办法知道是否正在读取属性文件和/或当前的类路径是什么?

回答:

修改您的测试类,如下所示:

@SpringApplicationConfiguration(classes = RistoreWebApplication.class, initializers = ConfigFileApplicationContextInitializer.class)

请参考以下内容:

http://docs.spring.io/spring-

boot/docs.old/current/api/org/springframework/boot/context/initializer/ConfigFileApplicationContextInitializer.html

通过存储库访问数据存储。

http://docs.spring.io/spring/docs/current/spring-framework-

reference/html/integration-

testing.html

@Autowired

YourRepository repository;

repository.save(Arrays.asList("value_1", "value_2", "value_3"));

以上是 Spring测试未从application.properties填充数据库配置 的全部内容, 来源链接: utcz.com/qa/405318.html

回到顶部