gpt4 book ai didi

java - 如何模拟包含可能引发 NPE 的变量的类

转载 作者:行者123 更新时间:2023-12-02 01:28:36 24 4
gpt4 key购买 nike

我有一个我想测试的登录演示者。我是单元测试的新手,我已经编写了一个基本测试来测试一个小功能。

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

登录Presenter测试

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,因为变量当前服务器变为空,因为其中没有保存 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()
}
}

最佳答案

你试过这个吗?

@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
whenever(serverInteractor.get()).thenReturn("http://fakeurl")
loginPresenter = LoginPresenter(
view, strategy, navigator, tokenRepository, localRepository, settingsInteractor,
analyticsManager, saveCurrentServer, saveAccountInteractor, factory, serverInteractor
)
}

whenever 方法来自 https://github.com/nhaarman/mockito-kotlin .

编辑:或者,如果您不想依赖mockito-kotlin,也可以使用 when 关键字-

`when`(serverInteractor.get()).thenReturn("http://fakeurl")

关于java - 如何模拟包含可能引发 NPE 的变量的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56467566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com