我正在使用Spock来测试一个使用JpaRepository的小型休息应用程序,但我似乎无法在我的测试中使用内存数据库。我承认我主要是关注exaples,我仍然不能理解所有正在发生的事情,所以任何帮助都会受到赞赏,任何解释都会受到极大的欢迎。
这是我到目前为止所拥有的。 我的控制器:
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 com.futsaltime.database.repositories.IFacilityRepository;
import com.futsaltime.mappers.FacilityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class FacilityService implements IFacilityService {
private IFacilityRepository facilityRepository;
@Autowired
public FacilityService(IFacilityRepository facilityRepository) {
this.facilityRepository = facilityRepository;
}
@Override
public Iterable<Facility> getFacilities() {
return facilityRepository.findAll();
}
@Override
public Facility add(FacilityDto facilityDto) {
Facility facility = FacilityMapper.toModel(facilityDto);
return facilityRepository.save(facility);
}
}
存储库
@Transactional
public interface IFacilityRepository extends CrudRepository<Facility, Long> {
Collection<Facility> findAllByFacilityName(String facilityName);
}
application.yml
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()
}
}
我读到 @ContextConfiguration 需要能够扫描要在自动装配中使用的类(这是真的)和SpringBootTest来选择applications.yml测试文件夹。我在那里设置了一个内存H2数据库但是我不确定当我使用 @DataJpaTest 时是否需要它。
如果我运行测试,则存储库不会自动装配,并且使用null创建服务,因此我获得了NPE。 有人可以帮助指出我缺少的东西或指向我指导甚至是关于注释的一些评论以及它们是否正确使用?