I'm trying to initialize (by mocking) two objects with the annotation @MockBean
It seems only to work if i call the method mock(className), but since i want to use the mocked class on multiple methods i don't want to keep repeating the same code in my test methods.
This is my test class:
@RunWith(MockitoJUnitRunner::class)
class WordServiceTest {
@MockBean
lateinit var wordRepositoryMock: WordRepository
@MockBean
private lateinit var wordMapperMock: WordMapper
@Test
fun findAllTest() {
// Error: lateinit property wordRepositoryMock has not been initialized
val wordService = WordService(wordRepositoryMock, wordMapperMock)
`when`(wordRepositoryMock.findAll()).thenReturn(listOf(
WordEntity(UUID.randomUUID(), "xxx"),
WordEntity(UUID.randomUUID(), "xxx")))
assertEquals(2, wordService.findAll().size)
}
@Test
fun wordExistsTest() {
// This works fine
val wordRepositoryMock = mock(WordRepository::class.java)
val wordMapperMock = mock(WordMapper::class.java)
val wordService = WordService(wordRepositoryMock, wordMapperMock)
val word = "xxx"
`when`(wordRepositoryMock.existsWordEntityByName(word)).thenReturn(true)
assertEquals(true, wordService.wordExists(word))
}
}
I don't want to use the Spring Boot @Autowired annotation because my spring application requires contexts which i don't want to load.
The error i'm getting:
lateinit property wordRepositoryMock has not been initialized
kotlin.UninitializedPropertyAccessException: lateinit property wordRepositoryMock has not been initialized
Aucun commentaire:
Enregistrer un commentaire