Skip to content

Commit

Permalink
tests: add unit tests for ParselyTracker initialization
Browse files Browse the repository at this point in the history
The `ParselyTracker#tearDown` method was added as singleton persisted between unit tests, causing some of them to fail. I could introduce a similar behavior using reflection, but I believe `internal tearDown` method is cleaner and is not problematic as not exposed to the client
  • Loading branch information
wzieba committed Jan 23, 2024
1 parent 451d061 commit 0478d9e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.parsely.parselyandroid

import android.content.Context
import kotlin.jvm.Throws
import org.jetbrains.annotations.TestOnly

/**
* Tracks Parse.ly app views in Android apps
Expand Down Expand Up @@ -111,4 +111,9 @@ public object ParselyTracker {
}
instance = ParselyTrackerInternal(siteId, flushInterval, context, dryRun)
}

@TestOnly
internal fun tearDown() {
instance = null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.parsely.parselyandroid

import org.junit.After
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment

@RunWith(RobolectricTestRunner::class)
class ParselyTrackerTest {

@Test(expected = ParselyNotInitializedException::class)
fun `given no prior initialization, when executing a method, throw the exception`() {
ParselyTracker.engagementIsActive()
}

@Test(expected = ParselyAlreadyInitializedException::class)
fun `given prior initialization, when initializing, throw an exception`() {
ParselyTracker.init(siteId = "", context = RuntimeEnvironment.getApplication())

ParselyTracker.init(siteId = "", context = RuntimeEnvironment.getApplication())
}

@Test
fun `given no prior initialization, when initializing, do not throw any exception`() {
ParselyTracker.init(siteId = "", context = RuntimeEnvironment.getApplication())
}

@Test
fun `given tracker initialized, when calling a method, do not throw any exception`() {
ParselyTracker.init(siteId = "", context = RuntimeEnvironment.getApplication())

ParselyTracker.engagementIsActive()
}

@After
fun tearDown() {
ParselyTracker.tearDown()
}
}

0 comments on commit 0478d9e

Please sign in to comment.