gpt4 book ai didi

spring-boot - Kotlin 使用存储库进行测试(spring-boot、kotlin、jersey、jax-rs)

转载 作者:行者123 更新时间:2023-11-28 21:37:54 25 4
gpt4 key购买 nike

我有可用的 kotlin 服务,但是当我尝试为其编写测试时,我卡住了,因为无论如何我都无法初始化所有服务...

@RunWith(SpringRunner::class)
class DataServiceTest {

@InjectMocks
private lateinit var DataService : DataService

@Mock
private lateinit var updateDataService: UpdateDataService

@Test
fun shouldUpdateCustomerEmail() {
DataService.setNewCustomerEmail("221722", ApiEmail("test@test.org"))

}

}

调用 DataService 类:

@Autowired
private lateinit var updateDataService: UpdateDataService

.....

fun setNewCustomerEmail(id: String, email: ApiEmail) {
updateDataService.setNewCustomerEmail(id, email)
}

调用 UpdateDataService 类:

@Service
open class UpdateDataService {

@Autowired
private lateinit var addressRepository: AddressRepository

fun setNewCustomerEmail(id: String, email: ApiEmail) {
val AddressList = getCustomerAddressList(id)
mapNewEmailToAddressList(AddressList, email)
cusAddressRepository.saveAll(AddressList)
}

fun getCustomerCusbaAddressList(id: String) : List<Address> {
return addressRepository.findAddressByCustomerId( id )
}

fun mapNewEmailToAddressList(cusbaAddressList : List<Address>, email: ApiEmail) {
AddressList.map { DataUtil.trimAddressFields( it ) }
AddressList.map { it.email = email.email }
}
}

我已经尝试了很多不同的 @RunWith() 属性和很多不同的方法来 Autowiring /模拟/InjectMocks 但无济于事。

问题:

目前使用此代码。当测试到达 UpdateDataService.class 时,AddressRepository 将被取消初始化。 返回 addressRepository.findAddressByCustomerId( id )

问题:

我如何连接所有这些服务,以便在应用程序运行时加载服务,同时测试知道如何连接这些服务和存储库?

最佳答案

使用Spring Boot Test可以轻松操作context

@RunWith(SpringRunner::class)
@SpringBootTest
class DataServiceTest

或提供一个@TestConfiguration

关于spring-boot - Kotlin 使用存储库进行测试(spring-boot、kotlin、jersey、jax-rs),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56216044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com