java - 如何模拟包含可以抛出 NPE 的变量的类
问题描述
我有一个我想测试的登录演示者。我是单元测试的新手,我已经编写了一个基本测试来测试一个小功能。
class LoginPresenter @Inject constructor(
private val view: LoginView,
private val strategy: CancelStrategy,
private val navigator: AuthenticationNavigator,
private val tokenRepository: TokenRepository,
private val localRepository: LocalRepository,
private val settingsInteractor: GetSettingsInteractor,
private val analyticsManager: AnalyticsManager,
private val saveCurrentServer: SaveCurrentServerInteractor,
private val saveAccountInteractor: SaveAccountInteractor,
private val factory: RocketChatClientFactory,
val serverInteractor: GetConnectingServerInteractor
) {
// TODO - we should validate the current server when opening the app, and have a nonnull get()
private var currentServer = serverInteractor.get()!!
private val token = tokenRepository.get(currentServer)
private lateinit var client: RocketChatClient
private lateinit var settings: PublicSettings
fun setupView() {
setupConnectionInfo(currentServer)
setupForgotPasswordView()
}
private fun setupConnectionInfo(serverUrl: String) {
currentServer = serverUrl
client = factory.get(currentServer)
settings = settingsInteractor.get(currentServer)
}
private fun setupForgotPasswordView() {
if (settings.isPasswordResetEnabled()) {
view.showForgotPasswordView()
}
}
fun authenticateWithUserAndPassword(usernameOrEmail: String, password: String) {
launchUI(strategy) {
view.showLoading()
try {
val token = retryIO("login") {
when {
settings.isLdapAuthenticationEnabled() ->
client.loginWithLdap(usernameOrEmail, password)
usernameOrEmail.isEmail() ->
client.loginWithEmail(usernameOrEmail, password)
else ->
client.login(usernameOrEmail, password)
}
}
val myself = retryIO("me()") { client.me() }
myself.username?.let { username ->
val user = User(
id = myself.id,
roles = myself.roles,
status = myself.status,
name = myself.name,
emails = myself.emails?.map { Email(it.address ?: "", it.verified) },
username = username,
utcOffset = myself.utcOffset
)
localRepository.saveCurrentUser(currentServer, user)
saveCurrentServer.save(currentServer)
localRepository.save(LocalRepository.CURRENT_USERNAME_KEY, username)
saveAccount(username)
saveToken(token)
analyticsManager.logLogin(
AuthenticationEvent.AuthenticationWithUserAndPassword,
true
)
view.saveSmartLockCredentials(usernameOrEmail, password)
navigator.toChatList()
}
} catch (exception: RocketChatException) {
when (exception) {
is RocketChatTwoFactorException -> {
navigator.toTwoFA(usernameOrEmail, password)
}
else -> {
analyticsManager.logLogin(
AuthenticationEvent.AuthenticationWithUserAndPassword,
false
)
exception.message?.let {
view.showMessage(it)
}.ifNull {
view.showGenericErrorMessage()
}
}
}
} finally {
view.hideLoading()
}
}
}
fun forgotPassword() = navigator.toForgotPassword()
private fun saveAccount(username: String) {
val icon = settings.favicon()?.let {
currentServer.serverLogoUrl(it)
}
val logo = settings.wideTile()?.let {
currentServer.serverLogoUrl(it)
}
val thumb = currentServer.avatarUrl(username, token?.userId, token?.authToken)
val account = Account(
settings.siteName() ?: currentServer,
currentServer,
icon,
logo,
username,
thumb
)
saveAccountInteractor.save(account)
}
private fun saveToken(token: Token) = tokenRepository.save(currentServer, token)
}
登录PresenterTest
class LoginPresenterTest {
lateinit var loginPresenter: LoginPresenter
private val view = mock(LoginView::class.java)
private val strategy = mock(CancelStrategy::class.java)
private val navigator = mock(AuthenticationNavigator::class.java)
private val tokenRepository = mock(TokenRepository::class.java)
private val localRepository = mock(LocalRepository::class.java)
private val settingsInteractor = mock(GetSettingsInteractor::class.java)
private val analyticsManager = mock(AnalyticsManager::class.java)
private val saveCurrentServer = mock(SaveCurrentServerInteractor::class.java)
private val saveAccountInteractor = mock(SaveAccountInteractor::class.java)
private val factory = mock(RocketChatClientFactory::class.java)
private val serverInteractor = mock(GetConnectingServerInteractor::class.java)
private val token = mock(Token::class.java)
private lateinit var settings: PublicSettings
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
loginPresenter = LoginPresenter(
view, strategy, navigator, tokenRepository, localRepository, settingsInteractor,
analyticsManager, saveCurrentServer, saveAccountInteractor, factory, serverInteractor
)
}
@Test
fun testAttach() {
loginPresenter.setupView()
assertNotNull(view)
}
}
但是在运行测试时,我得到 KotlinNullPointerException,因为变量 current server 变为空,因为其中没有保存 url。有什么办法可以避免它在运行测试期间为空。我曾尝试使用 elvis 运算符并修改 LoginPresenter 但我想知道还有其他方法可以工作。我只想用任何 url 字符串初始化当前服务器的值,以避免在运行测试期间出现 NPE。
private var currentServer = serverInteractor.get()?: "https://example.com"
我还有 SaveConnectingServerInteractor 和 GetConnectingServerInteractor 用于在身份验证时存储服务器 url。
class SaveConnectingServerInteractor @Inject constructor(
@ForAuthentication private val repository: CurrentServerRepository
) {
fun save(url: String) = repository.save(url)
}
class GetConnectingServerInteractor @Inject constructor(
@ForAuthentication private val repository: CurrentServerRepository
) {
fun get(): String? = repository.get()
fun clear() {
repository.clear()
}
}
解决方案
VishalHemnani 的回答确实对我有帮助,但如果有人不想使用https://github.com/nhaarman/mockito-kotlin并且正在使用官方的 mockito 库。在这种情况下,这将起作用
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
`when`(serverInteractor.get()).thenReturn("http://fakeurl")
loginPresenter = LoginPresenter(
view, strategy, navigator, tokenRepository, localRepository, settingsInteractor,
analyticsManager, saveCurrentServer, saveAccountInteractor, factory, serverInteractor
)
}
推荐阅读
- spring - spring boot @Value("${somevalue}") 注解是如何工作的?
- firebase - 使用 Firebase 身份验证在 Flutter 中注销用户不起作用
- javascript - ClickAwayListener 的回调函数在执行中途停止
- firefox - 无法删除 cookie - Firefox 拒绝过去的 cookie
- javascript - Microsoft Grap API - 在线会议。无法在 Microsoft Teams 中创建具有公共权限的实时事件
- google-bigquery - Bigquery - 按数组分组并求和
- c - 我尝试使用警报功能编写交流程序我第一次使用它,但我的输出不符合要求有人可以帮助我吗
- docusaurus - 如何在 docusaurus 中更改侧边栏的宽度
- rundeck - Rundeck 本地化问题。问题符号??????而不是日志输出部分中的俄语字母
- javascript - 在 JS 中插入 window.location 后 Firebase 数据库代码不起作用