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

Make MockKExtension open to allow custom extensions #1206

Closed
wants to merge 1 commit into from
Closed

Make MockKExtension open to allow custom extensions #1206

wants to merge 1 commit into from

Conversation

IgeNiaI
Copy link

@IgeNiaI IgeNiaI commented Jan 15, 2024

No description provided.

@Raibaz
Copy link
Collaborator

Raibaz commented Jan 16, 2024

I'm not sure of what use case this would support.

Do you have an example?

@IgeNiaI
Copy link
Author

IgeNiaI commented Jan 16, 2024

I have this utils object, note that date format is lazily initialized, and it takes current device time zone.

object DateTimeUtils {
    private val dateFormat by lazy {
        SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).apply {
            timeZone = TimeZone.getDefault()
        }
    }

    @Synchronized
    fun Date.asDateString(): String = dateFormat.format(this)
}

I have tests for this object and I can mock TimeZone.getDefault() to always return same value and thus make the tests resistant to time zone changes. But the Date.asDateString() method can be called from other app components that are executed by other tests. And because of that the SimpleDateFormat object initalizes with default time zone, and then the tests will fail.

I want to mock TimeZone.getDefault() before all tests are started to make them all consistent.

@IgeNiaI
Copy link
Author

IgeNiaI commented Jan 18, 2024

For some reason it didn't came to me that I should just mock DateTimeUtils in all other tests, even though I did that for other classes. My bad, I misused the feature.

@IgeNiaI IgeNiaI closed this Jan 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants