I am attempting to perform integration testing on services that I have developed. Part of the service involves using daos. At this point what I need to do is in my setup create some sort of connection to the persistent unit and then I can test my service.
I basically copy the persistence.xml and put it in src/test/resources and am trying
@Before
public void beginTransaction() {
emf = Persistence.createEntityManagerFactory(dao-test");
em = emf.createEntityManager();
}
@Test
public void testAccountDonation(){
AccountResult result = AccountService.getDonationAmount();
Assert.assertEquals("Success", result.getResultCode());
}
A connection could not be obtained for driver class “oracle.jdbc.driver.OracleDriver” and URL “jdbc:oracle:thin:@data-arctichome.arcww2.com:1521:orclgdb1”. You may have specified an invalid URL.
Here is my persistence unit.
<persistence-unit name="dao-test"
transaction-type="RESOURCE_LOCAL">
<properties>
<property name="openjpa.ConnectionDriverName"
value="oracle.jdbc.driver.OracleDriver" />
<property name="openjpa.ConnectionURL"
value="jdbc:oracle:thin:@xxxxxxxxx:orclgdb1" />
<property name="openjpa.ConnectionUserName" value="xxxxxx" />
<property name="openjpa.ConnectionPassword"
value="xxxxxx" />
</properties>
</persistence-unit>
Thanks.
Spring offers a lot of help for unit- and integration-testing, for example management of application contexts and transactions in integration tests, so rather than trying to create all the persistence- etc. stuff yourself for the tests in code, I’d suggest you read through the testing chapter (and maybe look for some examples in the ‘net) and use the functionality provided by Spring. It’s going to save you a ton of time and hair-pulling in the long run.