2016-06-02 15 views
1

ich zur Zeit keine Erklärung für das, was ich hier zu sehen - bitte lassen Sie mich wissen, wenn Sie weitere benötigen InformationenJUnit-Tests in die falsche Datenbank trotz Laden der richtigen Anwendungskontext Zugriff auf

ich eine Anwendung Kontextdatei habe

src/test/resources/applicationContext-jooq-test.xml 

, die wie folgt aussieht:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
     infrastructure --> 

    <!-- This is needed for the @Transactional annotation --> 
    <tx:annotation-driven transaction-manager="transactionManager" /> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
     up static resources in the ${webappRoot}/resources directory --> 
    <mvc:resources mapping="/resources/**" location="/resources/" /> 

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
     in the /WEB-INF/views directory --> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/views/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

    <context:component-scan base-package="com.mz.server" /> 

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://localhost:3306/mz_testdb?useSSL=false" /> 
     <property name="username" value="root" /> 
     <property name="password" value="" /> 
    </bean> 

    <!-- Configure Spring's transaction manager to use a DataSource --> 
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
     <property name="dataSource" ref="dataSource" /> 
    </bean> 

    <!-- Configure jOOQ's TransactionProvider as a proxy to Spring's transaction manager --> 
    <bean id="transactionProvider" 
     class="com.mz.server.SpringTransactionProvider"> 
    </bean> 

    <!-- Configure jOOQ's ConnectionProvider to use Spring's TransactionAwareDataSourceProxy, 
     which can dynamically discover the transaction context --> 
    <bean id="transactionAwareDataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy"> 
     <constructor-arg ref="dataSource" /> 
    </bean> 

    <bean class="org.jooq.impl.DataSourceConnectionProvider" name="connectionProvider"> 
     <constructor-arg ref="transactionAwareDataSource" /> 
    </bean> 

    <!-- Configure the DSL object, optionally overriding jOOQ Exceptions with Spring Exceptions --> 
    <bean id="dslContext" class="org.jooq.impl.DefaultDSLContext"> 
     <constructor-arg ref="config" /> 
    </bean> 

    <!-- Invoking an internal, package-private constructor for the example 
     Implement your own Configuration for more reliable behaviour --> 
    <bean class="org.jooq.impl.DefaultConfiguration" name="config"> 
     <property name="SQLDialect"><value type="org.jooq.SQLDialect">MYSQL</value></property> 
     <property name="connectionProvider" ref="connectionProvider" /> 
     <property name="transactionProvider" ref="transactionProvider" /> 
    </bean> 

</beans> 

wie Sie sehen ich bin klar definieren mz_testdb als meine Zieldatenbank.

Für meine JUnit-Tests habe ich ein AbstractRepositoryTest, die nichts tut, außer dass die Datenbank zu initialisieren und es zu einem nützlichen Zustand vor Prüfungen bringen beginnen:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = "classpath:/applicationContext-jooq-test.xml") 
public abstract class AbstractRepositoryTest { 

    private final static Logger LOGGER = Logger.getLogger(AbstractRepositoryTest.class.getName()); 

    private static DSLContext ctx; 

    public AbstractRepositoryTest() { 

    } 

    @BeforeClass 
    public static void setUpDatabase() { 

     LOGGER.debug("Setting up database for unit tests .."); 

     @SuppressWarnings("resource") 
     ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-jooq-test.xml"); 
     DataSource dataSource = (DataSource) applicationContext.getBean("dataSource"); 

     FlywayDbMain flywayDbMain = new FlywayDbMain(); 

     boolean databaseOkay = flywayDbMain.setUpCleanDatabase(dataSource); 

     AbstractRepositoryTest.ctx = (DSLContext) applicationContext.getBean("dslContext"); 

     org.junit.Assume.assumeTrue(databaseOkay); 

    } 

    protected static DSLContext getContext() { 
     return AbstractRepositoryTest.ctx; 
    } 
} 

Und natürlich bin ich mit einem Test vorbereitet:

public class ShopRepositoryTest extends AbstractRepositoryTest { 

    private final static Logger LOGGER = Logger.getLogger(ShopRepositoryTest.class.getName()); 

    private ShopRepository shopRepository; 

    private Long shop1Id; 

    private Long shop2Id; 

    private final static Long LANG_GERMAN = 1L; 

    private final static Long LANG_ENGLISH = 2L; 

    @Before 
    public void before() { 

     try { 

      this.shopRepository = new ShopRepository(AbstractRepositoryTest.getContext()); 

      LOGGER.info("Setting up database .."); 

      // Shop 1 

      this.shop1Id = this.shopRepository.createShop("Shop 1", 46.061785f, 16.464123f, "Europe/Vienna", LANG_GERMAN); 

      this.shopRepository.addSupportedLanguage(this.shop1Id, LANG_GERMAN); 
      this.shopRepository.addSupportedLanguage(this.shop1Id, LANG_ENGLISH); 

      // Shop 2 

      this.shop2Id = this.shopRepository.createShop("Shop 2", 46.061785f, 16.464123f, "Europe/Vienna", LANG_GERMAN); 

      this.shopRepository.addSupportedLanguage(this.shop2Id, LANG_GERMAN); 
      this.shopRepository.addSupportedLanguage(this.shop2Id, LANG_ENGLISH); 

     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @Test 
    public void deleteAllItemDetails() { 
     // ... 
    } 

    @After 
    public void after() { 
     LOGGER.debug("All done."); 
    } 

} 

Nun wird der Fehler, den ich bekommen habe ist folgendes:

Caused by: java.sql.SQLSyntaxErrorException: Table 'mz_db.shop' doesn't exist 
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:686) 
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:663) 
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:653) 

Ich habe das Schiff aus meinem Code entfernt, aber ich sehe es einfach nicht. wo und warum bin ich plötzlich mit meiner dev datenbank mz_db sprechen? Wenn ich überall obwohl der Code trete ich sehe ich sehe, dass die URL tatsächlich korrekt ist:

enter image description here

In der Tat gibt es reden über mz_db aber das ist alles in

/src/main/webapp/WEB-INF/spring/applicationContext-jooq.xml 

Natürlich, wenn Ich erstelle mz_db Ich bekomme diese Ausnahme nicht, aber alle Daten werden in diese Tabelle geschrieben, die gerade nicht das ist, was ich will.

Ich bin sehr neugierig, wie ich es geschafft habe, so etwas zu tun und es muss etwas Dummes sein, das ich nicht sehe.

Was passiert hier?


Ich habe bereits versucht zu

  • Reinigen Sie alle Projekte
  • löschen target/ Ordner
  • Neustart

Antwort

0

jOOQ per se nicht kümmern uns um Ihre JDBC-Verbindungs-URL nicht Eklipse. Dies ist nur die Standarddatenbank, die verwendet wird, wenn Tabellen nicht vollständig qualifiziert sind. Aber jOOQ qualifiziert Tabellen standardmäßig mit Schemanamen (= Datenbanknamen in MySQL). Wenn Sie beispielsweise jOOQ-Tabellen aus Ihrer Datenbank mz_db generieren und Abfragen für eine mz_testdb-Verbindung ausführen, qualifiziert das generierte SQL die Tabelle mz_db.shop weiterhin so, wie sie ursprünglich generiert wurde.

Es gibt zwei Möglichkeiten, dieses Problem zu beheben:

  1. deaktivieren voll qualifizierenden Tabellen Settings.renderSchema
  2. konfigurieren Schema Mapping Settings.renderMappingmz_db zu mz_testdb

Letztere auch hier dokumentiert neu zu schreiben verwendet, ist: http://www.jooq.org/doc/latest/manual/sql-building/dsl-context/runtime-schema-mapping

+0

Oh! Ich hätte nicht erwartet, dass dies von jooq kommt. Ich denke, ich werde in diesem Fall den 'renderMapping'-Ansatz verwenden! Danke für deine Hilfe Lukas! – displayname

+1

Im Zweifelsfall können Sie immer [Debug-Protokollierung aktivieren] (http://www.jooq.org/doc/latest/manual/sql-execution/logging) sehen, um das von jOOQ generierte SQL zu sehen ... –