Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit Testing with AWS Amplify - Mocking #3641

Open
DigiValetMobiles opened this issue Sep 23, 2024 · 3 comments
Open

Unit Testing with AWS Amplify - Mocking #3641

DigiValetMobiles opened this issue Sep 23, 2024 · 3 comments
Labels
question General question

Comments

@DigiValetMobiles
Copy link

I'm currently facing challenges in unit testing my authentication functions that utilize the AWS Amplify library, specifically regarding the AWSCognitoAuthSession class. The issue arises because this class has an internal constructor property, making it inaccessible in my test classes.

Service:
aws-auth-cognito

Environment:
SDK Version: 2.20.0

Problem:
I need to test my sign-in function, which uses the Amplify Auth sign-in function and fetches the user session. However, I cannot mock the AWSCognitoAuthSession effectively due to its constructor's visibility constraints.

Questions:
What strategies can I use to mock AWSCognitoAuthSession in my unit tests?
Are there any recommended practices for testing functions that rely on AWS Amplify's Auth module?
Would it be possible to consider making constructors more accessible for testing purposes in future updates?

Testing framework:
JUnit, Mockk

Programming Language:
Kotlin

If there are any specific examples or resources that could help clarify how to effectively test AWS Amplify Auth functionality, I would greatly appreciate it!

@github-actions github-actions bot added pending-triage Issue is pending triage pending-maintainer-response Issue is pending response from an Amplify team member labels Sep 23, 2024
@phantumcode
Copy link
Member

@DigiValetMobiles Thanks for submitting the issue. Can you provide additional details on what specific sign in function you're testing and how you've implemented that sign in function?

@github-actions github-actions bot removed the pending-maintainer-response Issue is pending response from an Amplify team member label Sep 23, 2024
@phantumcode phantumcode added question General question pending-maintainer-response Issue is pending response from an Amplify team member labels Sep 23, 2024
@github-actions github-actions bot removed the pending-triage Issue is pending triage label Sep 23, 2024
@DigiValetMobiles
Copy link
Author

Here's a code snippet of my sign-in function:

fun signIn(username: String, password: String) = flow {
    Amplify.Auth.signIn(username = username, password = password)
    val session = Amplify.Auth.fetchAuthSession() as AWSCognitoAuthSession
    emit(DigiAuthResult.Success(session.toDigiAuthResponse()))
}.catch {
    emit(it.toDigiAuthError())
}

I am trying to create a mock object of AWSCognitoAuthSession to test the success scenario of my signIn function using dummy values. However, I'm encountering issues when attempting to mock it like this:

val mockObject = mockk<AWSCognitoAuthSession>(relaxed = true)

It would be helpful if you could provide @VisibleForTesting annotations on any private or internal classes, as this could simplify the mocking process.

Thank you!

@phantumcode
Copy link
Member

@DigiValetMobiles making AWSCognitoAuthSession visible for testing isn't a viable option. It would also require other classes it depends on such as AuthSessionResult to be visible for testing.

Looking at your sign-in function, there isn't any business logic involved so I would reconsider what the value of unit testing this function and reconsider it as a integration test instead.

Are you trying to unit test the toDigiAuthResponse()? or are you unit testing the emit logic?

@github-actions github-actions bot removed the pending-maintainer-response Issue is pending response from an Amplify team member label Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question General question
Projects
None yet
Development

No branches or pull requests

2 participants