2017-04-12 105 views
2

我想配置我的JUnit测试运行在不同于运行应用程序的数据库上的数据库上。我一直在做这个小时,但迄今为止没有成功。尝试使用application.properties文件和spring配置文件进行配置,但没有任何工作。那么如何才能做到呢?任何帮助将不胜感激。使测试数据库上运行Spring JUnit测试

这里是我的测试文件:

@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> 

这是我的属性文件:

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)} 
+0

http://stackoverflow.com/a/34756569/3641067 –

+0

如上所述,我试过了,但它只是忽略了.properties文件中的设置 –

+0

嗯,我不太确定。我有点新鲜,我想用MocMvc来执行我的休息请求。通过删除@AutoConfigure它不再工作。我应该自己配置它,而不是让它为我做? –

回答

1

你有没有可能使用Spring Boot?它为您提供了基于Servlet的“普通”部署,并将其部署到现有的Tomcat实例中。我最后一次纯粹的春季会议太费了。

我可以给你一个尼克斯与Spring Boot的答案的例子,并假设你使用的是Maven。

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

为了您的高效环境,在src > main > resources中创建一个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 

正如你所看到的,对于test荷兰国际集团则不需要重复一切,只是它改变了的东西。

现在,您可以通过启动应用程序来测试您的应用程序在生产模式下运行。在你的控制台你应该看到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数据库。

+0

我使用的是Spring Boot,并且在删除“@AutoConfigureMockMvc”注释之后做了你建议的操作,并获得了以下异常(请参阅编辑的问题)。我应该如何配置mockmvc? –

+0

如果向JUnit测试中添加'@ AutoConfigureMockMvc',它会起作用吗?我更新了我的例子。 – hesch

+0

nope,如果我补充说它不再在测试数据库上运行 –