1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 6 package org.mockitousage.annotation; 7 8 import org.junit.After; 9 import org.junit.Test; 10 import org.mockito.InjectMocks; 11 import org.mockito.Mock; 12 import org.mockito.MockitoAnnotations; 13 import org.mockito.configuration.AnnotationEngine; 14 import org.mockito.configuration.DefaultMockitoConfiguration; 15 import org.mockito.internal.configuration.ConfigurationAccess; 16 import org.mockito.internal.configuration.IndependentAnnotationEngine; 17 import org.mockitoutil.TestBase; 18 19 import static junit.framework.TestCase.*; 20 21 public class DeprecatedAnnotationEngineApiTest extends TestBase { 22 23 @After goBackToDefaultConfiguration()24 public void goBackToDefaultConfiguration() { 25 ConfigurationAccess.getConfig().overrideAnnotationEngine(null); 26 } 27 28 class SimpleTestCase { 29 @InjectMocks Tested tested = new Tested(); 30 @Mock Dependency mock; 31 } 32 33 class Tested { 34 Dependency dependency; setDependency(Dependency dependency)35 public void setDependency(Dependency dependency) { 36 this.dependency = dependency; 37 } 38 } 39 40 class Dependency {} 41 42 @Test shouldInjectMocksIfThereIsNoUserDefinedEngine()43 public void shouldInjectMocksIfThereIsNoUserDefinedEngine() throws Exception { 44 //given 45 AnnotationEngine defaultEngine = new DefaultMockitoConfiguration().getAnnotationEngine(); 46 ConfigurationAccess.getConfig().overrideAnnotationEngine(defaultEngine); 47 SimpleTestCase test = new SimpleTestCase(); 48 49 //when 50 MockitoAnnotations.initMocks(test); 51 52 //then 53 assertNotNull(test.mock); 54 assertNotNull(test.tested.dependency); 55 assertSame(test.mock, test.tested.dependency); 56 } 57 58 @Test shouldRespectUsersEngine()59 public void shouldRespectUsersEngine() throws Exception { 60 //given 61 AnnotationEngine customizedEngine = new IndependentAnnotationEngine() { /**/ }; 62 ConfigurationAccess.getConfig().overrideAnnotationEngine(customizedEngine); 63 SimpleTestCase test = new SimpleTestCase(); 64 65 //when 66 MockitoAnnotations.initMocks(test); 67 68 //then 69 assertNotNull(test.mock); 70 assertNull(test.tested.dependency); 71 } 72 } 73