In addition to Mike and Savva's suggestions, I wanted to point to our LinkRest
unit tests. Here is a base class (ignore the Jersey part, just focus on Cayenne
and Derby) [1].
* It manages a lifecycle of a Derby DB instance via simple DerbyManager,
ensuring filesystem folders are cleaned up between the runs.
* Cayenne XML is DataNode-less [2], expecting you to define the DataSource via
API in the unit test (as well as in the main app .. not in this example though).
* ServerRuntime creation procedure includes making an in-memory DataSource and
a unit test SchemaUpdateStrategy:
CAYENNE_RUNTIME = new ServerRuntimeBuilder()
.addConfig("cayenne-linkrest-tests.xml")
.addModule(new Module() {
// Java 7; for Java 8 you can use a lambda for the Module
@Override
public void configure(Binder binder) {
binder.bind(SchemaUpdateStrategy.class).to(CreateIfNoSchemaStrategy.class);
}
})
.jdbcDriver("org.apache.derby.jdbc.EmbeddedDriver")
.url("jdbc:derby:target/derby;create=true")
.build();
Hope this helps.
Andrus
[1]
https://github.com/nhl/link-rest/blob/master/src/test/java/com/nhl/link/rest/it/fixture/JerseyTestOnDerby.java
[2]
https://github.com/nhl/link-rest/blob/master/src/test/resources/cayenne-linkrest-tests.xml
Andrus
> On Jan 22, 2016, at 9:50 PM, Hugi Thordarson <[email protected]> wrote:
>
> Ah, sorry about that… I’m using the current 4.0 snapshots.
>
> - hugi
>
>
>
>> On 22. jan. 2016, at 18:47, Michael Gentry <[email protected]> wrote:
>>
>> Which version of Cayenne are you using?
>>
>>
>> On Fri, Jan 22, 2016 at 1:25 PM, Hugi Thordarson <[email protected]> wrote:
>>
>>> Hi all,
>>>
>>> I’m writing some tests for my DataObjects. Before the tests run, I’d like
>>> to provision an h2 database and switch my model’s connection to it for the
>>> purpose of testing.
>>>
>>> What’s the preferred way to switch the connection programmatically?
>>>
>>> Cheers,
>>> - hugi
>