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.basicapi; 7 8 import org.junit.Test; 9 import org.mockito.Mockito; 10 import org.mockito.exceptions.base.MockitoException; 11 import org.mockito.exceptions.verification.SmartNullPointerException; 12 import org.mockitousage.IMethods; 13 import org.mockitoutil.TestBase; 14 15 import java.lang.annotation.Retention; 16 import java.lang.annotation.RetentionPolicy; 17 import java.util.LinkedList; 18 import java.util.List; 19 import java.util.Set; 20 21 import static org.assertj.core.api.Assertions.assertThat; 22 import static org.junit.Assert.assertFalse; 23 import static org.junit.Assert.assertNotNull; 24 import static org.junit.Assert.assertTrue; 25 import static org.junit.Assert.fail; 26 import static org.mockito.Mockito.RETURNS_SMART_NULLS; 27 import static org.mockito.Mockito.mock; 28 import static org.mockito.Mockito.when; 29 import static org.mockito.Mockito.withSettings; 30 31 @SuppressWarnings("unchecked") 32 public class MocksCreationTest extends TestBase { 33 34 private class HasPrivateConstructor {} 35 36 @Test should_create_mock_when_constructor_is_private()37 public void should_create_mock_when_constructor_is_private() { 38 assertNotNull(Mockito.mock(HasPrivateConstructor.class)); 39 } 40 41 @Test should_combine_mock_name_and_smart_nulls()42 public void should_combine_mock_name_and_smart_nulls() { 43 //given 44 IMethods mock = mock(IMethods.class, withSettings() 45 .defaultAnswer(RETURNS_SMART_NULLS) 46 .name("great mockie")); 47 48 //when 49 IMethods smartNull = mock.iMethodsReturningMethod(); 50 String name = mock.toString(); 51 52 //then 53 assertThat(name).contains("great mockie"); 54 //and 55 try { 56 smartNull.simpleMethod(); 57 fail(); 58 } catch(SmartNullPointerException e) {} 59 } 60 61 @Test should_combine_mock_name_and_extra_interfaces()62 public void should_combine_mock_name_and_extra_interfaces() { 63 //given 64 IMethods mock = mock(IMethods.class, withSettings() 65 .extraInterfaces(List.class) 66 .name("great mockie")); 67 68 //when 69 String name = mock.toString(); 70 71 //then 72 assertThat(name).contains("great mockie"); 73 //and 74 assertTrue(mock instanceof List); 75 } 76 77 @Test should_specify_mock_name_via_settings()78 public void should_specify_mock_name_via_settings() { 79 //given 80 IMethods mock = mock(IMethods.class, withSettings().name("great mockie")); 81 82 //when 83 String name = mock.toString(); 84 85 //then 86 assertThat(name).contains("great mockie"); 87 } 88 89 @Test should_scream_when_spy_created_with_wrong_type()90 public void should_scream_when_spy_created_with_wrong_type() { 91 //given 92 List list = new LinkedList(); 93 try { 94 //when 95 mock(List.class, withSettings().spiedInstance(list)); 96 fail(); 97 //then 98 } catch (MockitoException e) {} 99 } 100 101 @SuppressWarnings({"CheckReturnValue", "MockitoUsage"}) 102 @Test should_allow_creating_spies_with_correct_type()103 public void should_allow_creating_spies_with_correct_type() { 104 List list = new LinkedList(); 105 mock(LinkedList.class, withSettings().spiedInstance(list)); 106 } 107 108 @Test should_allow_inline_mock_creation()109 public void should_allow_inline_mock_creation() { 110 when(mock(Set.class).isEmpty()).thenReturn(false); 111 } 112 113 @Retention(RetentionPolicy.RUNTIME) 114 @interface SomeAnnotation {} 115 116 @SomeAnnotation static class Foo {} 117 118 @Test should_strip_annotations()119 public void should_strip_annotations() { 120 Foo withAnnotations = mock(Foo.class); 121 Foo withoutAnnotations = mock(Foo.class, withSettings().withoutAnnotations()); 122 123 //expect: 124 assertTrue(withAnnotations.getClass().isAnnotationPresent(SomeAnnotation.class)); 125 assertFalse(withoutAnnotations.getClass().isAnnotationPresent(SomeAnnotation.class)); 126 } 127 } 128