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:
JavaScript
x
@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:
JavaScript
lateinit property wordRepositoryMock has not been initialized
kotlin.UninitializedPropertyAccessException: lateinit property wordRepositoryMock has not been initialized
Dependencies:
JavaScript
dependencies {
testImplementation("org.springframework.security:spring-security-test")
testImplementation ('org.springframework.boot:spring-boot-starter-test')
testImplementation("org.junit.jupiter:junit-jupiter:5.6.2")
testImplementation "org.junit.jupiter:junit-jupiter-params:5.5.2"
testImplementation "org.junit.jupiter:junit-jupiter-api:5.6.2"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.6.2"
testImplementation("io.rest-assured:spring-mock-mvc:4.0.0")
testImplementation("io.mockk:mockk:1.9.3")
testImplementation "org.testcontainers:postgresql:1.11.3"
testImplementation "org.springframework.kafka:spring-kafka-test:2.2.7.RELEASE"
runtimeOnly('org.postgresql:postgresql')
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation "org.mockito:mockito-junit-jupiter:3.3.3"
}
Advertisement
Answer
I solved this problem by calling initMocks method in setUp()
JavaScript
@BeforeEach
fun setUp() {
MockitoAnnotations.initMocks(this)
.
}