diff --git a/parsely/src/main/java/com/parsely/parselyandroid/ParselyTracker.kt b/parsely/src/main/java/com/parsely/parselyandroid/ParselyTracker.kt index 5b0212b8..0468d589 100644 --- a/parsely/src/main/java/com/parsely/parselyandroid/ParselyTracker.kt +++ b/parsely/src/main/java/com/parsely/parselyandroid/ParselyTracker.kt @@ -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 @@ -111,4 +111,9 @@ public object ParselyTracker { } instance = ParselyTrackerInternal(siteId, flushInterval, context, dryRun) } + + @TestOnly + internal fun tearDown() { + instance = null + } } diff --git a/parsely/src/test/java/com/parsely/parselyandroid/ParselyTrackerTest.kt b/parsely/src/test/java/com/parsely/parselyandroid/ParselyTrackerTest.kt new file mode 100644 index 00000000..f9fb57c7 --- /dev/null +++ b/parsely/src/test/java/com/parsely/parselyandroid/ParselyTrackerTest.kt @@ -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() + } +}