我最近更新到ActiveJDBC 2.1,以便使用activejdbc.properties来外化数据库属性,因此我们不必将数据库用户名/密码签入SVN。
将“activejdbc.property”文件放在src / main / resources中作为主代码是完美的。现在的目标是用“activejdbc.property”替换测试目录src / test / resources中的“database.property”文件,这样它就可以指向文件系统上的相同数据库配置文件。
在测试目录中进行此更改后,执行gradle构建时会收到错误(gradle clean build)。这是我们看到的例外情况:
`“org.javalite.activejdbc.InitException:java.io.FileNotFoundException:\ database.properties(系统找不到指定的文件)”
任何想法为什么这适用于主目录但不适用于测试?
Stacktrace:es / main / com / brookdale / model / UnitOfMeasure.class **************************** END INSTRUMENTATION ***** *********************** ...:汇编:compileTestJava:processTestResources:testClasses:test
com.brookdale.model.ActualChargeTest > unitQuantityMustBeGreaterThanZero FAILED
org.javalite.activejdbc.InitException: java.io.FileNotFoundException: \database.properties (The system cannot find the file specified)
Caused by:
java.io.FileNotFoundException: \database.properties (The system cannot find the file specified)
... more tests ...
com.brookdale.service.RelationshipServiceTest > updateContactRel_GivenValidInfo_
RecordIsInserted FAILED
org.javalite.activejdbc.InitException: java.io.FileNotFoundException: \datab
ase.properties (The system cannot find the file specified)
Caused by:
java.io.FileNotFoundException: \database.properties (The system cannot f
ind the file specified)
49 tests completed, 25 failed, 7 skipped
:test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
BUILD FAILED`
看来你没有正确命名文件:文件名不是activejdbc.property
,而是activejdbc.properties
。
此外,Java类加载器不保证如果在类路径上找到多个文件,它们将首先加载哪个文件。如果您想在测试环境中使用不同的JDBC属性,请按照以下文档进行操作:http://javalite.io/database_connection_management#multiple-environments-property-file-method
以下是具有此实现的示例项目:https://github.com/javalite/simple-example/