1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockitousage.annotation; 6 7 import org.junit.Test; 8 import org.junit.runner.RunWith; 9 import org.mockito.InjectMocks; 10 import org.mockito.junit.MockitoJUnitRunner; 11 12 import static org.junit.Assert.assertNotNull; 13 import static org.junit.Assert.assertSame; 14 import static org.mockito.Mockito.mock; 15 import static org.mockito.Mockito.spy; 16 17 @RunWith(MockitoJUnitRunner.class) 18 public class InjectionOfInlinedMockDeclarationTest { 19 20 @InjectMocks private Receiver receiver; 21 @InjectMocks private Receiver spiedReceiver = spy(new Receiver()); 22 23 private Antenna oldAntenna = mock(Antenna.class); 24 private Antenna satelliteAntenna = mock(Antenna.class); 25 private Antenna antenna = mock(Antenna.class, "dvbtAntenna"); 26 private Tuner tuner = spy(new Tuner()); 27 28 @Test mock_declared_fields_shall_be_injected_too()29 public void mock_declared_fields_shall_be_injected_too() throws Exception { 30 assertNotNull(receiver.oldAntenna); 31 assertNotNull(receiver.satelliteAntenna); 32 assertNotNull(receiver.dvbtAntenna); 33 assertNotNull(receiver.tuner); 34 } 35 36 @Test unnamed_mocks_should_be_resolved_withe_their_field_names()37 public void unnamed_mocks_should_be_resolved_withe_their_field_names() throws Exception { 38 assertSame(oldAntenna, receiver.oldAntenna); 39 assertSame(satelliteAntenna, receiver.satelliteAntenna); 40 } 41 42 @Test named_mocks_should_be_resolved_with_their_name()43 public void named_mocks_should_be_resolved_with_their_name() throws Exception { 44 assertSame(antenna, receiver.dvbtAntenna); 45 } 46 47 48 @Test inject_mocks_even_in_declared_spy()49 public void inject_mocks_even_in_declared_spy() throws Exception { 50 assertNotNull(spiedReceiver.oldAntenna); 51 assertNotNull(spiedReceiver.tuner); 52 } 53 54 // note that static class is not private !! 55 static class Receiver { 56 Antenna oldAntenna; 57 Antenna satelliteAntenna; 58 Antenna dvbtAntenna; 59 Tuner tuner; 60 tune()61 public boolean tune() { return true; } 62 } 63 64 private static class Antenna { } 65 private static class Tuner { } 66 67 } 68