2017-09-02 5 views
0

Ich habe Probleme mit JUnit-Tests, um zu überprüfen, ob eine Datenbank korrekt mit dem UCanAccess-Treiber funktioniert. Wenn ich den Server normalerweise in IntelliJ IDEA laufen, wie ich höre, dass die Datenbank-Pool richtig eingerichtet ist:play 2.6.3 erkennt Datenbank aus der application.conf während der Ausführung, aber nicht während der Tests

[info] application - Creating Pool for datasource 'default' 
[info] p.a.d.DefaultDBApi - Database [default] connected at jdbc:ucanaccess://c:/Users/me/Documents/testdb.accdb 

Allerdings, wenn ich JUnit-Tests durch IntelliJ laufen, ich folgende Fehlermeldung erhalten:

Configuration error: Configuration error[Driver not found: [org.h2.Driver}]] 

at play.api.Configuration$.configError(Configuration.scala:155) 
at play.api.Configuration.reportError(Configuration.scala:984) 
at play.api.db.DefaultDatabase$$anonfun$driver$1.apply(Databases.scala:114) 
at play.api.db.DefaultDatabase$$anonfun$driver$1.apply(Databases.scala:108) 
... 

Ich nehme an, dass der obige Fehler von Play verursacht wird, der standardmäßig die Standarddatenbankkonfiguration verwendet, dh eine h2-Datenbank.

Wie kann ich es so machen, dass JUnit-Tests meinen Datenbanktreiber erkennen und nicht standardmäßig eine h2-Datenbank verwenden?

Tests:

import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 
import play.db.Database; 
import play.db.Databases; 
import play.db.evolutions.Evolution; 
import play.db.evolutions.Evolutions; 

import java.sql.Connection; 

import static org.junit.Assert.assertTrue; 


public class ApplicationTest { 

    Database database; 


    @Before 
    public void setupDatabase() { 
     // Gets default database in this case 
     database = Databases.inMemory(); 

     System.out.println(database.getConnection()); 
     Evolutions.applyEvolutions(database, Evolutions.forDefault(new Evolution(
       1, 
       "create table teste (id bigint not null, name varchar(255));", 
       "drop table test;" 
     ))); 
    } 


    @After 
    public void shutdownDatabase() { 
     Evolutions.cleanupEvolutions(database); 
     database.shutdown(); 
    } 

    @Test 
    public void databaseCRUD() throws Exception { 
     Connection conn = database.getConnection(); 
     conn.prepareStatement("insert into test values (10, 'testing')").execute(); 
     // Make sure that there is a tenth element in the table 
     assertTrue(
       conn.prepareStatement("select * from test where id = 10 and name = 'testing'") 
         .executeQuery().next() 
     ); 
    } 
} 

Antwort

0

Sie die Datenbank erstellen können den Treiber Sie benötigen. Hier ist ein Postgres Beispiel:

val dbUrl = sys.env.getOrElse("DATABASE_URL", "jdbc:postgresql://localhost:5432/app?user=user&password=password") 
val database = Databases("org.postgresql.Driver", dbUrl, "testingzzz") 

Verwenden Sie die richtige URL und Fahrer, und Sie sind gut zu gehen.

Verwandte Themen