[英]NoSuchFieldError when trying to run a jUnit test with Spring
[英]Make Spring JUnit Test run on test database
我想將我的JUnit測試配置為在與運行Application的數據庫不同的數據庫上運行。 我已經這樣做了好幾個小時但到目前為止沒有成功。 嘗試使用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實例中。 我最后一次簡單的春季會議太過分了。
我可以舉一個例子來說明Nikos用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數據庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.