diff --git a/examples/simple/pom.xml b/examples/simple/pom.xml index 19df04d52ed..54ba98e1c97 100644 --- a/examples/simple/pom.xml +++ b/examples/simple/pom.xml @@ -38,5 +38,16 @@ ${project.version} true + + + junit + junit + test + + + org.mockito + mockito-core + test + diff --git a/examples/simple/src/test/java/coffee/CoffeeMakerTest.java b/examples/simple/src/test/java/coffee/CoffeeMakerTest.java new file mode 100644 index 00000000000..310a188a193 --- /dev/null +++ b/examples/simple/src/test/java/coffee/CoffeeMakerTest.java @@ -0,0 +1,54 @@ +/** + * Copyright (C) 2015 Square, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package coffee; + +import dagger.Module; +import dagger.ObjectGraph; +import dagger.Provides; +import javax.inject.Inject; +import javax.inject.Singleton; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.mockito.Mockito; + +public class CoffeeMakerTest { + @Inject CoffeeMaker coffeeMaker; + @Inject Heater heater; + + @Before public void setUp() { + ObjectGraph.create(new TestModule()).inject(this); + } + + @Module( + includes = DripCoffeeModule.class, + injects = CoffeeMakerTest.class, + overrides = true + ) + static class TestModule { + @Provides @Singleton Heater provideHeater() { + return Mockito.mock(Heater.class); + } + } + + @Test public void testHeaterIsTurnedOnAndThenOff() { + Mockito.when(heater.isHot()).thenReturn(true); + coffeeMaker.brew(); + Mockito.verify(heater, Mockito.times(1)).on(); + Mockito.verify(heater, Mockito.times(1)).off(); + } +} diff --git a/pom.xml b/pom.xml index 89f93319e3a..7d08bede229 100644 --- a/pom.xml +++ b/pom.xml @@ -51,6 +51,7 @@ 1.4 0.13 0.4 + 1.10.19 @@ -114,6 +115,11 @@ truth ${truth.version} + + org.mockito + mockito-core + ${mockito.version} +