1 /* 2 * Copyright (c) 2017 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockitousage.bugs.creation; 6 7 import org.junit.Test; 8 import org.junit.experimental.runners.Enclosed; 9 import org.junit.runner.RunWith; 10 import org.mockito.MockitoAnnotations; 11 import org.mockito.Spy; 12 13 import java.util.Random; 14 15 @RunWith(Enclosed.class) 16 public class ConstructorInvokingMethodShouldNotRaiseExceptionTest { 17 18 public static class WithDumbMethod { 19 @Spy 20 HasConstructorInvokingMethod hasConstructorInvokingMethod; 21 22 @Test 23 public void should_be_able_to_create_spy() throws Exception { 24 MockitoAnnotations.initMocks(this); 25 } 26 27 private static class HasConstructorInvokingMethod { 28 public HasConstructorInvokingMethod() { someMethod(); } 29 30 void someMethod() { } 31 } 32 } 33 34 public static class UsingMethodObjectReferenceResult { 35 @Spy 36 HasConstructorInvokingMethod hasConstructorInvokingMethod; 37 38 @Test 39 public void should_be_able_to_create_spy() throws Exception { 40 MockitoAnnotations.initMocks(this); 41 } 42 43 private static class HasConstructorInvokingMethod { 44 private final boolean doesIt; 45 public HasConstructorInvokingMethod() { 46 doesIt = someMethod().contains("yup"); 47 } 48 49 String someMethod() { return "tada!"; } 50 } 51 } 52 53 public static class UsingMethodPrimitiveResult { 54 @Spy 55 HasConstructorInvokingMethod hasConstructorInvokingMethod; 56 57 @Test 58 public void should_be_able_to_create_spy() throws Exception { 59 MockitoAnnotations.initMocks(this); 60 } 61 62 private static class HasConstructorInvokingMethod { 63 private final boolean doesIt; 64 public HasConstructorInvokingMethod() { 65 doesIt = someMethod(); 66 } 67 68 boolean someMethod() { return new Random().nextBoolean(); } 69 } 70 } 71 } 72