Spring test does not populate database configuration from application.properties

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 the integration test in this class using the spring test of the following example in Integration Testing the 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 class of the application, which is the entry point of the api. According to this web page, " @SpringApplicationConfiguration annotation will result in logic for reading spring specific boot configurations, properties, etc." I understand that this will load database connection information in my .properties applications:

 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 configuration that runs 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 decided that I would have another properties file under src/test/resources based on Spring's answer JUnit Testing Properties File . So I did this and added the @PropertySource("classpath:application-test.properties") annotation to my test class, but still getting the same error. Is there a way to find out if a properties file is being read and / or what is the current classpath?

0
source share
2 answers

Change your test class as shown below:

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

See below:

Spring download, read yml properties with integration test

http://docs.spring.io/spring-boot/docs.old/current/api/org/springframework/boot/context/initializer/ConfigFileApplicationContextInitializer.html

EDIT: regarding the comment below.

Access to the data warehouse through the 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
source

@PropertySource intended to be used in the @Configuration class, not a test class.

In your test class, you should use @TestPropertySource .

+1
source

All Articles