У меня есть ведущий входа в систему, который я хочу протестировать.Я новичок в модульном тестировании, и я написал базовый тест для тестирования небольших функций.
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)
}
LoginPresenterTest
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()
}
}