• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * 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.testing;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.junit.Assert.assertThrows;
21 
22 import android.app.Application;
23 import androidx.test.ext.junit.runners.AndroidJUnit4;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.robolectric.annotation.Config;
27 
28 @HiltAndroidTest
29 @RunWith(AndroidJUnit4.class)
30 public final class HiltAndroidRuleTest {
31   public static final class NonHiltTest {}
32 
33   @Test
34   @Config(application = HiltTestApplication.class)
testMissingHiltAndroidTest_fails()35   public void testMissingHiltAndroidTest_fails() throws Exception {
36     IllegalStateException exception =
37         assertThrows(
38             IllegalStateException.class,
39             () -> new HiltAndroidRule(new NonHiltTest()));
40     assertThat(exception)
41           .hasMessageThat()
42           .isEqualTo(
43               "Expected dagger.hilt.android.testing.HiltAndroidRuleTest$NonHiltTest to be "
44                   + "annotated with @HiltAndroidTest.");
45 
46   }
47 
48   @Test
49   @Config(application = Application.class)
testNonHiltTestApplication_fails()50   public void testNonHiltTestApplication_fails() throws Exception {
51     IllegalStateException exception =
52         assertThrows(
53             IllegalStateException.class,
54             () -> new HiltAndroidRule(HiltAndroidRuleTest.this));
55     assertThat(exception)
56           .hasMessageThat()
57           .isEqualTo(
58               "Hilt test, dagger.hilt.android.testing.HiltAndroidRuleTest, must use a Hilt test "
59                   + "application but found android.app.Application. To fix, configure the test to "
60                   + "use HiltTestApplication or a custom Hilt test application generated with "
61                   + "@CustomTestApplication.");
62 
63   }
64 
65   @Test
66   @Config(application = HiltAndroidRuleTestApp.class)
testHiltAndroidApplication_fails()67   public void testHiltAndroidApplication_fails() throws Exception {
68     IllegalStateException exception =
69         assertThrows(
70             IllegalStateException.class,
71             () -> new HiltAndroidRule(HiltAndroidRuleTest.this));
72     assertThat(exception)
73           .hasMessageThat()
74           .isEqualTo(
75               "Hilt test, dagger.hilt.android.testing.HiltAndroidRuleTest, cannot use a "
76                   + "@HiltAndroidApp application but found "
77                   + "dagger.hilt.android.testing.HiltAndroidRuleTestApp. To fix, configure the "
78                   + "test to use HiltTestApplication or a custom Hilt test application generated "
79                   + "with @CustomTestApplication.");
80 
81   }
82 }
83