lundi 20 mai 2019

Kotlin tests with repository ( spring-boot, kotlin, jersey, jax-rs )

I have working kotlin service, but when i try to write test for it i get stuck because i cant get all my services initialized no matter what...

@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"))

    }

}

which calls DataService class:

@Autowired
private lateinit var updateDataService: UpdateDataService

.....

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

which calls UpdateDataService class:

@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 }
    }
}

I've tried lots of different @RunWith() properties and lots of different ways to Autowire / Mock / InjectMocks but to no avail.

Problem:

At the moment with this code. AddressRepository will be uninitialized when test gets to UpdateDataService.class in line. return addressRepository.findAddressByCustomerId( id )

Question:

How do i wire all these services in such way that services are loaded when app is running, but also test would know how to wire these services and repository ?

Aucun commentaire:

Enregistrer un commentaire