1+ package com.formbricks.android.network
2+
3+ import android.content.Context
4+ import androidx.test.core.app.ApplicationProvider
5+ import androidx.test.ext.junit.runners.AndroidJUnit4
6+ import androidx.test.platform.app.InstrumentationRegistry
7+ import com.formbricks.android.model.user.PostUserBody
8+ import org.junit.Assert.*
9+ import org.junit.Before
10+ import org.junit.Test
11+ import org.junit.runner.RunWith
12+
13+ @RunWith(AndroidJUnit4 ::class )
14+ class FormbricksApiServiceInstrumentedTest {
15+ private lateinit var context: Context
16+ private lateinit var apiService: FormbricksApiService
17+
18+ @Before
19+ fun setUp () {
20+ context = ApplicationProvider .getApplicationContext()
21+ apiService = FormbricksApiService ()
22+ // You may want to initialize with a test server or mock URL
23+ apiService.initialize(" https://example.com" , isLoggingEnabled = false )
24+ }
25+
26+ @Test
27+ fun testInitialization () {
28+ // This test just verifies that initialization does not throw
29+ try {
30+ apiService.initialize(" https://example.com" , isLoggingEnabled = false )
31+ } catch (e: Exception ) {
32+ fail(" Initialization should not throw: ${e.message} " )
33+ }
34+ }
35+
36+ @Test
37+ fun testGetEnvironmentStateObject_handlesErrorGracefully () {
38+ val result = apiService.getEnvironmentStateObject(" dummy-environment-id" )
39+ assertTrue(result.isFailure)
40+ result.exceptionOrNull()?.let { e ->
41+ println (" Exception caught as expected: ${e.message} " )
42+ }
43+ }
44+
45+ @Test
46+ fun testPostUser_handlesErrorGracefully () {
47+ // This should fail gracefully since the URL is unreachable
48+ val dummyBody = PostUserBody (" dummy-user-id" , null )
49+ val result = apiService.postUser(" dummy-environment-id" , dummyBody)
50+ assertTrue(result.isFailure)
51+ }
52+
53+ // Add more integration-style tests as needed, e.g.:
54+ // - testGetEnvironmentStateObject_withMockServer
55+ // - testPostUser_withMockServer
56+ // These would require a running test server or a mock web server
57+ }
0 commit comments