首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尽管加载了正确的应用程序上下文,JUnit仍然测试访问错误的数据库。

问尽管加载了正确的应用程序上下文,JUnit仍然测试访问错误的数据库。
EN

Stack Overflow用户
提问于 2016-06-02 14:39:21
回答 1查看 491关注 0票数 1

我目前无法解释我在这里看到的情况-如果你需要更多的信息,请告诉我。

我有一个应用程序上下文文件

代码语言:javascript
复制
src/test/resources/applicationContext-jooq-test.xml

看起来是这样的:

代码语言:javascript
复制
<?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>

如您所见,我清楚地将mz_testdb定义为我的目标数据库。

对于我的JUnit测试,我有一个AbstractRepositoryTest,它除了初始化数据库并在测试开始之前将其带入一个有用的状态之外,什么也不做:

代码语言:javascript
复制
@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;
    }
}

当然,我正在准备一项考试:

代码语言:javascript
复制
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.");
    }

}

现在,我遇到的错误如下:

代码语言:javascript
复制
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)

我已经把飞船从我的代码中调试出来了,但是我没有看到它。我在哪里和为什么突然与我的dev数据库mz_db交谈?如果我在任何地方看到代码,我就会发现url实际上是正确的:

​

​

事实上,有关于mz_db的讨论,但这都是在

代码语言:javascript
复制
/src/main/webapp/WEB-INF/spring/applicationContext-jooq.xml

当然,如果我创建mz_db,我不会得到这个异常,但是所有的数据都会写入到这个表中,这不是我想要的。

我很好奇我是如何做到这样的事情的,这一定是一些我看不到的愚蠢的事情。

这里发生了什么事?

我已经试过了

  • 清理所有项目
  • 删除target/文件夹
  • 重启Eclipse
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-02 16:35:22

jOOQ本身并不关心您的。当表没有完全限定时,这只是要使用的默认数据库。但默认情况下,jOOQ完全限定具有模式名称的表(= MySQL中的数据库名称)。因此,如果从jOOQ数据库生成mz_db表并对mz_testdb连接运行查询,则生成的mz_db.shop表仍将完全限定为最初生成的表。

有两种方法可以解决这个问题:

  1. 使用Settings.renderSchema禁用完全限定表
  2. 使用Settings.renderMapping配置模式映射,将mz_db重写为mz_testdb

后者也记录在这里:http://www.jooq.org/doc/latest/manual/sql-building/dsl-context/runtime-schema-mapping

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37594966

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档