1 /* <lambda>null2 * Copyright (C) 2020 The Dagger Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package dagger.hilt.android.simpleKotlin 18 19 import android.os.Build 20 import androidx.appcompat.app.AppCompatActivity 21 import androidx.test.core.app.ActivityScenario 22 import com.google.common.truth.Truth.assertThat 23 import dagger.hilt.android.AndroidEntryPoint 24 import dagger.hilt.android.testing.BindValue 25 import dagger.hilt.android.testing.HiltAndroidRule 26 import dagger.hilt.android.testing.HiltAndroidTest 27 import dagger.hilt.android.testing.HiltTestApplication 28 import javax.inject.Inject 29 import org.junit.Rule 30 import org.junit.Test 31 import org.junit.runner.RunWith 32 import org.robolectric.RobolectricTestRunner 33 import org.robolectric.annotation.Config 34 35 /** 36 * Example local unit test, which will execute on the development machine (host). 37 * 38 * See [testing documentation](http://d.android.com/tools/testing). 39 */ 40 @HiltAndroidTest 41 @RunWith(RobolectricTestRunner::class) 42 // Robolectric requires Java9 to run API 29 and above, so use API 28 instead 43 @Config(sdk = [Build.VERSION_CODES.P], application = HiltTestApplication::class) 44 class SimpleTest { 45 @get:Rule val rule = HiltAndroidRule(this) 46 47 @BindValue val bindStr = "STRING_BINDING" 48 49 @Inject @JvmField 50 var str: String? = null 51 52 @AndroidEntryPoint 53 class TestActivity : AppCompatActivity() 54 55 @Test 56 fun testBindValue() { 57 assertThat(str).isNull() 58 rule.inject() 59 assertThat(str).isEqualTo(bindStr) 60 } 61 62 @Test 63 fun verifyMainActivity() { 64 ActivityScenario.launch(MainActivity::class.java).use { scenario -> 65 scenario.onActivity { activity -> 66 assertThat(activity::class.java.getSuperclass()?.getSimpleName()) 67 .isEqualTo("Hilt_MainActivity") 68 assertThat(activity.viewModel.model).isNotNull() 69 assertThat(activity.viewModel.name).isNotNull() 70 } 71 } 72 } 73 74 @Test 75 fun verifyTestActivity() { 76 ActivityScenario.launch(TestActivity::class.java).use { scenario -> 77 scenario.onActivity { activity -> 78 assertThat(activity::class.java.getSuperclass()?.getSimpleName()) 79 .isEqualTo("Hilt_SimpleTest_TestActivity") 80 } 81 } 82 } 83 } 84