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.bugs.injection; 6 7 import org.junit.Before; 8 import org.junit.Ignore; 9 import org.junit.Test; 10 import org.mockito.InjectMocks; 11 import org.mockito.Mock; 12 import org.mockito.MockitoAnnotations; 13 14 import static org.junit.Assert.assertNotNull; 15 16 // issue 229 : @Mock fields in super test class are not injected on @InjectMocks fields 17 public class ParentTestMockInjectionTest { 18 19 @Test injectMocksShouldInjectMocksFromTestSuperClasses()20 public void injectMocksShouldInjectMocksFromTestSuperClasses() { 21 ImplicitTest it = new ImplicitTest(); 22 MockitoAnnotations.initMocks(it); 23 24 assertNotNull(it.daoFromParent); 25 assertNotNull(it.daoFromSub); 26 assertNotNull(it.sut.daoFromParent); 27 assertNotNull(it.sut.daoFromSub); 28 } 29 30 @Ignore 31 public static abstract class BaseTest { 32 @Mock protected DaoA daoFromParent; 33 } 34 35 @Ignore("JUnit test under test : don't test this!") 36 public static class ImplicitTest extends BaseTest { 37 @InjectMocks private TestedSystem sut = new TestedSystem(); 38 39 @Mock private DaoB daoFromSub; 40 41 @Before setup()42 public void setup() { 43 MockitoAnnotations.initMocks(this); 44 } 45 46 @Test noNullPointerException()47 public void noNullPointerException() { 48 sut.businessMethod(); 49 } 50 } 51 52 public static class TestedSystem { 53 private DaoA daoFromParent; 54 private DaoB daoFromSub; 55 businessMethod()56 public void businessMethod() { 57 daoFromParent.doQuery(); 58 daoFromSub.doQuery(); 59 } 60 } 61 62 63 public static class DaoA { doQuery()64 public void doQuery() { } 65 } 66 67 public static class DaoB { doQuery()68 public void doQuery() { } 69 } 70 71 } 72