NullPointerException Groovy 单元测试 Java Spring

NullPointerException Groovy Unit Test Java Spring

提问人:Bugi 提问时间:10/24/2023 更新时间:10/24/2023 访问量:36

问:

PharmacyRepository最终变为Null,我不知道为什么?我试图调试几个小时,但无法做到这一点......有人可以帮我解决这个问题吗?

class PharmacyRepositoryTest extends AbstractIntegrationContainerBaseTest {

    @Autowired
    PharmacyRepository pharmacyRepository

    void setup() {
        pharmacyRepository.deleteAll()
    }

    def "test saving Pharmacy"() {
        given:
        String address = "서울 특별시 성북구 종암동"
        String name = "은혜 약국"
        double latitude = 36.11
        double longitude = 128.11

        def pharmacy = Pharmacy.builder()
                .pharmacyAddress(address)
                .pharmacyName(name)
                .latitude(latitude)
                .longitude(longitude)
                .build()

        when:
        def savedPharmacy = pharmacyRepository.save(pharmacy)

        then:
        savedPharmacy.pharmacyAddress == address
        savedPharmacy.pharmacyName == name
        savedPharmacy.latitude == latitude
        savedPharmacy.longitude == longitude
    }

    def "test saving multiple Pharmacies"() {
        given:
        String address = "서울 특별시 성북구 종암동"
        String name = "은혜 약국"
        double latitude = 36.11
        double longitude = 128.11

        def pharmacy = Pharmacy.builder()
                .pharmacyAddress(address)
                .pharmacyName(name)
                .latitude(latitude)
                .longitude(longitude)
                .build()

        when:
        pharmacyRepository.saveAll([pharmacy])
        def result = pharmacyRepository.findAll()

        then:
        result.size() == 1
        result[0].pharmacyAddress == address
        result[0].pharmacyName == name
        result[0].latitude == latitude
        result[0].longitude == longitude
    }
}

这是日志,很明显,它说我尝试自动连接的存储库是 Null,因此无法调用其方法。


Cannot invoke method deleteAll() on null object
java.lang.NullPointerException: Cannot invoke method deleteAll() on null object
    at dev.be.pharma.pharmacy.repository.PharmacyRepositoryTest.setup(PharmacyRepositoryTest.groovy:14)


Cannot invoke method deleteAll() on null object
java.lang.NullPointerException: Cannot invoke method deleteAll() on null object
    at dev.be.pharma.pharmacy.repository.PharmacyRepositoryTest.setup(PharmacyRepositoryTest.groovy:14)

java spring groovy nullpointerexception

评论

0赞 daggett 10/24/2023
stackoverflow.com/questions/19896870/......
0赞 Bugi 10/26/2023
井。。如您所见,我没有使用 new 关键字实例化存储库。另外,存储库是一个接口......

答: 暂无答案