【发布时间】:2018-01-02 16:22:03
【问题描述】:
我正在为 Spring Boot Rest 控制器编写测试。这个休息控制器将一些值写入数据库。
我想使用 Spring 为此测试提供的内存数据库。根据this doc我必须用@DataJpaTest注释测试类,这会导致这个错误:
java.lang.IllegalStateException: Failed to load ApplicationContext
在错误堆栈跟踪的更下方,我看到引发了以下异常:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Failed to replace DataSource with an embedded database for tests. If you want an embedded database please put a supported one on the classpath or tune the replace attribute of @AutoconfigureTestDatabase.
这是我正在研究的测试类:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@DataJpaTest
public class AuthenticationControllerFTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private AuthenticationManager authenticationManager;
@Autowired
private WebApplicationContext context;
@Autowired
private Filter springSecurityFilterChain;
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(context)
.addFilters(springSecurityFilterChain).build();
}
@Test
public void testCreate() throws Exception {
String exampleUserInfo = "{\"name\":\"Salam12333\",\"username\":\"test@test1.com\",\"password\":\"Salam12345\"}";
RequestBuilder requestBuilder = MockMvcRequestBuilders
.post("/signup")
.accept(MediaType.APPLICATION_JSON).content(exampleUserInfo)
.contentType(MediaType.APPLICATION_JSON);
MvcResult result = mockMvc.perform(requestBuilder).andReturn();
MockHttpServletResponse response = result.getResponse();
int status = response.getStatus();
Assert.assertEquals("http response status is wrong", 200, status);
}
}
是什么导致了这个错误?
编辑 1
这是我application.properties的内容:
spring.datasource.username = hello
spring.datasource.password = hello
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/myproject?useSSL=false
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type=TRACE
logging.level.org.springframework.web=DEBUG
server.port = 8443
server.ssl.key-store = classpath:tomcat.keystore
server.ssl.key-store-password = hello
server.ssl.key-password = hello
server.ssl.enabled = true
server.ssl.key-alias=myproject
编辑 2
我在pom.xml 中添加了以下内容:
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>test</scope>
</dependency>
我创建了application-test.properties,内容如下:
spring.datasource.username= root
spring.datasource.password= password
spring.datasource.driver-class-name= org.h2.Driver
spring.datasource.url= jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
- 用户名和密码是什么?我应该在哪里设置它们?
- 我还在测试类中添加了
@ActiveProfiles("test"),当我运行测试时,我收到一个错误,其中包括这一行:
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
【问题讨论】:
-
"如果你想要一个嵌入式数据库,请将支持的数据库放在类路径中或调整 @AutoconfigureTestDatabase 的替换属性"。那么,您是否在类路径中放置了嵌入式数据库?
-
我将我的
application.properties文件内容添加到原始帖子中。 -
application.properties 不影响类路径。您需要将 h2 或 hsqldb 添加到您的 pom.xml。
-
我加了,请看编辑2。
-
spring.datasource.url= jdbc:h2:mem:db;DB_CLOSE_DELAY=-1-- h2 不是 hsqldb。请参阅下面的答案..慢慢来,小心。
标签: java spring testing spring-boot