ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

java-使Spring JUnit Test在测试数据库上运行

2019-11-11 13:10:23  阅读:330  来源: 互联网

标签:spring-boot junit hibernate java spring-mvc


我想将我的JUnit测试配置为在与应用程序运行所在的数据库不同的数据库上运行.我已经这样做了几个小时,但到目前为止还没有成功.尝试使用application.properties文件和spring config文件对其进行配置,但是没有任何效果.那么如何才能做到这一点呢?任何帮助将不胜感激.

这是我的测试文件:

@RunWith(SpringRunner.class)
@SpringBootTest(properties = "spring.profiles.active=test")
public class TestContactController extends AbstractTest {

    @Autowired
    private MockMvc mvc;

    @Test
    @Rollback
    public void testAddContact() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/contact/add").contentType(MediaType.APPLICATION_JSON)
            .content("{\n" +
                    "\t\"authCode\": \"daca824a0bf04d038543373adfdb2c8f\",\n" +
                    "\t\"requestData\":{\n" +
                    "\t\t\"firstName\": \"Max\",\n" +
                    "\t\t\"lastName\": \"Mustermann\",\n" +
                    "\t\t\"category\": {\n" +
                    "\t\t\t\"id\": " + categoryId + "\n" +
                    "\t\t}, \"partOfOrgUnit\": {\n" +
                    "\t\t\t\"id\": " + companyId + "\n" +
                    "\t\t}\n" +
                    "\t}\n" +
                    "}"))
            .andExpect(status().isOk())
            .andExpect(content().string(equalTo("{\"success\":true,\"message\":\"SUCCESS: Contact added successfully\"}")));
    }
}

我的持久性xml,带有两个数据库:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
         version="2.0">
    <persistence-unit name="sab_pu">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3307/projectdb?serverTimezone=UTC"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="root"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="javax.persistence.validation.mode" value="NONE"/>
        </properties>
    </persistence-unit>
    <persistence-unit name="sab_test_pu">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3307/testdb?serverTimezone=UTC"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="root"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="javax.persistence.validation.mode" value="NONE"/>
        </properties>
    </persistence-unit>
</persistence>

这是我的.properties文件:

spring.datasource.url=jdbc:mysql://localhost:3307/testdb?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

例外:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.SAB.test.testsController.TestContactController':
Unsatisfied dependency expressed through field 'mvc'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type 'org.springframework.test.web.servlet.MockMvc' available: expected at least 1 bean which qualifies as autowire candidate. 
Dependency annotations:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

解决方法:

您有可能使用Spring Boot吗?它还为您提供了基于Servlet的“普通”部署到已经存在的Tomcat实例中.我最近的春季会议太遥远了.

我可以举一个使用Spring Boot回答Nikos的例子,并假设您正在使用Maven.

在Spring Boot中,可以使用application.properties文件配置大多数内容.这也包括persistence.xml文件的设置.

为了您的生产环境,在src>中创建一个application.properties文件.主要>具有此类内容的资源.

spring.datasource.url=jdbc:mysql://localhost:3307/projectdb?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto: create-drop
spring.jpa.show-sql=true

测试环境需要一个具有此内容的名为application-test.properties的文件.

spring.datasource.url=jdbc:mysql://localhost:3307/testdb?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

如您所见,对于测试,您无需重复所有操作,只需重复更改的内容即可.

现在,您只需启动应用程序即可测试您的应用程序是否可以在生产模式下运行.在控制台中,您应该看到Spring Boot使用MySQL.如果要使用测试设置,请添加-Dspring.profiles.active = test作为JVM参数.

完成此步骤后,让我们切换到如下所示的JUnit测试.

@RunWith(SpringRunner.class)
@SpringBootTest(properties = "spring.profiles.active=test")
@AutoConfigureMockMvc
public class MeTest {
    @Test
    public void testMe() {
        System.out.println("Hello World!");
    }
}

@SpringBootTest设置要测试的当前概要文件,并开始JUnit测试运行.

这只是解决问题的一种可能方法.我希望它能对您有所帮助,并且一切正常,因为我的工作计算机上没有MySQL数据库.

标签:spring-boot,junit,hibernate,java,spring-mvc
来源: https://codeday.me/bug/20191111/2020242.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有