spring test not populating database configuration

2019-06-02 04:35发布

问题:

In my spring boot application, I have the following class that calls a stored procedure

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

Now I would like to run integration test on this class using spring test following example in Integration Testing a 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);
    }
}

Here RistoreWebApplication is the name of the main app class which is the entry point of the api. According to that web page, "The @SpringApplicationConfiguration annotation will trigger logic for reading Spring Boot specific configurations, properties, and so on." My understanding is this will load database connection information in my application.properties as well:

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

However, when I run this test, I got UnsupportedOperationException: The application must supply JDBC connections. How exactly do I load the db connection config running the test? I am using spring boot 1.3

EDIT since application.properties lives in src/main/resources while my test classes are in src/test/java, I figured I'd have another property file under src/test/resources based on the answer to Spring jUnit Testing properties file. So I did that and added annotation @PropertySource("classpath:application-test.properties") in my test class, but still getting the same error. Is there a way to know whether the property file is being read and/or what is the current classpath?

回答1:

Modify your test class like below:

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

Please refer below:

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

EDIT: with respect to below comment.

Access your datastore via your Repository.

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


回答2:

@PropertySource is for use on an @Configuration class, not on a test class.

On your test class, you should instead use @TestPropertySource.