我是测试的初学者,现在我遇到了一个无法克服的问题。
@SpringBootTest
ExampleMakerSpec extends Specification {
@Autowired @Subject ExampleMaker exampleMaker
@Autowired
ExampleRepository exampleRepository
def EXAMPLE_VARIABLE = "Example"
@Transactional
def "example() trying to do somehthing" () {
when: "trying to make some examples"
Examples examples = exampleMaker.createExamples(examples)
then: "get examples sizes and saves them to database"
examples.size == 7
我的示例制造商看起来是这样的:
@Component
public class ExampleMaker {
@Autowired
ExampleRepository exampleRepository;
public void createExamples() {
exampleRepository.save(Examples);
}
}
和CRUD存储库:
@Repository
public interface exampleRepository extends CrudRepository<Example, Long> {
}
但我总是收到
java.lang.NullPointerException位于exampleRepository.save(Examples(.行
因此,由于某些原因,测试无法找到存储库。我需要这里的帮助来了解缺少了什么。
您需要在测试类中自动连接exampleRepository。因此,当创建测试类ExampleMakerSpec
时,Spring会查找并注入ExampleRepository