spring test不从application.properties填充数据库配置

时间:2016-11-03 20:16:50

标签: jdbc spring-boot spring-test

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

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;
    }
}

现在,我想在Integration Testing a Spring Boot Application中使用spring test下面的示例对此类运行集成测试。

@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是主app类的名称,它是api的入口点。根据该网页,&#34; @SpringApplicationConfiguration注释将触发读取Spring Boot特定配置,属性等的逻辑。&#34;我的理解是这将在我的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。我究竟如何加载运行测试的db连接配置?我正在使用spring boot 1.3

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

2 个答案:

答案 0 :(得分:2)

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

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

请参阅以下内容:

Spring boot, read yml properties via integration test case

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"));

答案 1 :(得分:1)

@PropertySource适用于@Configuration课程,而非测试课程。

在测试课程中,您应该使用@TestPropertySource