• 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 example.hilt;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.Context;
22 import android.os.Build;
23 import androidx.test.core.app.ApplicationProvider;
24 import androidx.test.ext.junit.runners.AndroidJUnit4;
25 import dagger.hilt.android.testing.BindValue;
26 import dagger.hilt.android.testing.HiltAndroidRule;
27 import dagger.hilt.android.testing.HiltAndroidTest;
28 import dagger.hilt.android.testing.HiltTestApplication;
29 import example.common.CoffeeLogger;
30 import example.common.CoffeeMaker;
31 import example.common.Pump;
32 import javax.inject.Inject;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.robolectric.annotation.Config;
37 
38 /** Tests using a fake pump. */
39 @HiltAndroidTest
40 @Config(sdk = {Build.VERSION_CODES.P}, application = HiltTestApplication.class)
41 @RunWith(AndroidJUnit4.class)
42 public final class CoffeeAppFakePumpTest {
43   public @Rule HiltAndroidRule rule = new HiltAndroidRule(this);
44 
45   @Inject CoffeeMaker maker;
46   @Inject CoffeeLogger logger;
47 
48   @BindValue Pump fakePump = () -> logger.log("=> => fake pumping => =>");
49 
50   @Test
testApplicationClass()51   public void testApplicationClass() throws Exception {
52     assertThat((Context) ApplicationProvider.getApplicationContext())
53         .isInstanceOf(HiltTestApplication.class);
54   }
55 
56   @Test
testLogs()57   public void testLogs() throws Exception {
58     rule.inject();
59     assertThat(logger.logs()).isEmpty();
60     maker.brew();
61     assertThat(logger.logs())
62         .containsExactly(
63             "~ ~ ~ heating ~ ~ ~",
64             "=> => fake pumping => =>",
65             " [_]P coffee! [_]P ")
66         .inOrder();
67   }
68 }
69