Я конвертирую некоторый код Java
в Kotlin
и получаю исключение UnfinishedStubbingException
для кода ниже:
@RunWith(SpringRunner::class)
@SpringBootTest(classes = [FormsApplication::class])
class AccountResourceIntTest {
@Autowired
lateinit var userRepository: UserRepository
@Autowired
lateinit var authorityRepository: AuthorityRepository
@Autowired
lateinit var userService: UserService
@Autowired
lateinit var passwordEncoder: PasswordEncoder
@Autowired
lateinit var httpMessageConverters: Array<HttpMessageConverter<*>>
@Autowired
lateinit var exceptionTranslator: ExceptionTranslator
@Mock
lateinit var mockUserService: UserService
@Mock
lateinit var mockMailService: MailService
private lateinit var restMvc: MockMvc
private lateinit var restUserMockMvc: MockMvc
@Before
fun setup() {
MockitoAnnotations.initMocks(this)
doNothing().`when`(mockMailService).sendActivationEmail(any()) <-- exception
...
}
...
}
Трассировка стека:
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at com.application.web.rest.AccountResourceIntTest.setup(AccountResourceIntTest.kt:82)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, which is not supported
3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed
at com.application.service.UserServiceIntTest.init(UserServiceIntTest.kt:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
...
MailService
class
помечен как open
.
@Service
open class MailService {
...
@Async
fun sendActivationEmail(user: User) {
log.debug("Sending activation email to '{}'", user.email)
sendEmailFromTemplate(user, "mail/activationEmail", "email.activation.title")
}
}
Код Java
, из которого я преобразую этот тест:
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
doNothing().when(mockMailService).sendActivationEmail(any());
...
}
Ошибка указывает на отсутствующий оператор thenReturn
, но в оригинале это не требуетсяJava
код.Что здесь происходит?