Iri*_*ram 4 java junit hibernate spring-mvc spring-boot
我想将 JUnit 测试配置为在与应用程序运行的数据库不同的数据库上运行。我已经这样做了几个小时,但到目前为止还没有成功。尝试使用 application.properties 文件和 spring 配置文件配置它,但没有任何效果。那么如何才能实现这一点呢?任何帮助将不胜感激。
这是我的测试文件:
@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。
对于您的生产环境,创建一个包含如下内容的application.properties文件。src > main > resources
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
正如您所看到的,对于testing,您不需要重复所有内容,只需重复更改的内容即可。
现在,您只需启动应用程序即可测试应用程序是否在生产模式下运行。在您的控制台中,您应该看到 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当前配置文件设置为test并启动 JUnit 测试运行。
这只是解决您的问题的一种可能方法。我希望它对您有所帮助并且一切正常,因为我的工作机器上没有 MySQL 数据库。
| 归档时间: | 
 | 
| 查看次数: | 11349 次 | 
| 最近记录: |