Rest 您将如何测试使用带有Spock和内存数据库的JpaRepository的服务?
我正在使用Spock测试一个使用JpaRepository的小型rest应用程序,但在测试中使用内存中的数据库似乎有问题。我承认,我主要是在遵循一些例子,我仍然不太了解正在发生的一切,因此任何帮助都将不胜感激,任何作为奖金的解释都将受到极大欢迎 这就是我目前所拥有的。 我的控制器:Rest 您将如何测试使用带有Spock和内存数据库的JpaRepository的服务?,rest,spring-boot,h2,spock,Rest,Spring Boot,H2,Spock,我正在使用Spock测试一个使用JpaRepository的小型rest应用程序,但在测试中使用内存中的数据库似乎有问题。我承认,我主要是在遵循一些例子,我仍然不太了解正在发生的一切,因此任何帮助都将不胜感激,任何作为奖金的解释都将受到极大欢迎 这就是我目前所拥有的。 我的控制器: import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntit
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Collection;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
@RestController
@RequestMapping("facilities")
public class FacilityController {
private IFacilityService facilityService;
@Autowired
public FacilityController(IFacilityService facilityService) {
this.facilityService = facilityService;
}
@RequestMapping(path = "list", method = GET)
public ResponseEntity<Collection<Facility>> list() {
Collection<Facility> facilities = new ArrayList<>();
facilityService.getFacilities().forEach(facilities::add);
return ResponseEntity.ok().body(facilities);
}
@RequestMapping(path = "add", method = POST)
public ResponseEntity<?> add(@RequestBody FacilityDto facilityDto) {
Facility facility = facilityService.add(facilityDto);
return ResponseEntity.ok().body(facility);
}
}
还有这个测试
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ContextConfiguration
import spock.lang.Specification
@ContextConfiguration
@SpringBootTest
@DataJpaTest
class FacilityServiceTest extends Specification {
FacilityService facilityService
@Autowired
IFacilityRepository facilityRepository;
def setup() {
facilityService = new FacilityService(facilityRepository)
}
def "retrieves all the facilities saved in the database"() {
given:
when:
def result = facilityService.getFacilities()
then:
result.isEmpty()
}
}
我了解到@ContextConfiguration需要能够扫描自动连接中使用的类(这是真的吗)和SpringBootTest来选择测试文件夹中的applications.yml。我有一个在内存中建立H2数据库,但我不确定当我使用@DataJpaTest时是否需要它
如果我运行测试,存储库不是自动连接的,服务是用空值创建的,因此我得到一个NPE。
有人能帮我指出我遗漏了什么,或者给我指一个指南,甚至是一些注释的内容,以及它们是否正确使用吗?如果您使用的是spock 1.1,那么在使用
@springbootest
时,您不需要添加@ContextConfiguration
。您需要使用@repository
注释存储库。您可能希望注入FacilityService
,而不是在测试中构造它。@LeonardBrünings我添加了存储库注释,并尝试自动连接服务,而不是使用安装方法,但仍然是一样的。也许我做错了一些更基本的事情。你的类路径中有spock core和spock spring 1.1吗?没有,我只有org.spockframework:spock core:1.1-groovy-2.4。好的,我会的
@Transactional
public interface IFacilityRepository extends CrudRepository<Facility, Long> {
Collection<Facility> findAllByFacilityName(String facilityName);
}
server:
port: 8000
endpoints:
enabled: true
sensitive: false
spring:
datasource: # DataSource settings: set here your own configurations for the database
url: jdbc:mysql://localhost:3306/futsaltime
type: com.zaxxer.hikari.HikariDataSource
username: root
password: root
testWhileIdle: true # Keep the connection alive if idle for a long time (needed in production)
validationQuery: SELECT 1
jpa:
show-sql: true
hibernate:
ddl-auto: create
# Use spring.jpa.properties.* for Hibernate native properties (the prefix is stripped before adding them to the entity manager)
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL57Dialect # To force the engine to InnoDB
format_sql: true
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ContextConfiguration
import spock.lang.Specification
@ContextConfiguration
@SpringBootTest
@DataJpaTest
class FacilityServiceTest extends Specification {
FacilityService facilityService
@Autowired
IFacilityRepository facilityRepository;
def setup() {
facilityService = new FacilityService(facilityRepository)
}
def "retrieves all the facilities saved in the database"() {
given:
when:
def result = facilityService.getFacilities()
then:
result.isEmpty()
}
}