• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.stubbing;
6 
7 import static org.junit.Assert.assertNotNull;
8 import static org.mockito.ArgumentMatchers.*;
9 import static org.mockito.Mockito.*;
10 
11 import java.util.List;
12 
13 import org.junit.Test;
14 import org.mockito.internal.stubbing.answers.ClonesArguments;
15 import org.mockitoutil.TestBase;
16 
17 public class CloningParameterTest extends TestBase {
18 
19     @Test
shouldVerifyEvenIfArgumentsWereMutated()20     public void shouldVerifyEvenIfArgumentsWereMutated() throws Exception {
21 
22         // given
23         EmailSender emailSender = mock(EmailSender.class, new ClonesArguments());
24 
25         // when
26         businessLogic(emailSender);
27 
28         // then
29         verify(emailSender).sendEmail(1, new Person("Wes"));
30     }
31 
businessLogic(EmailSender emailSender)32     private void businessLogic(EmailSender emailSender) {
33         Person person = new Person("Wes");
34         emailSender.sendEmail(1, person);
35         person.emailSent();
36     }
37 
38     @Test
shouldReturnDefaultValueWithCloningAnswer()39     public void shouldReturnDefaultValueWithCloningAnswer() throws Exception {
40 
41         // given
42         EmailSender emailSender = mock(EmailSender.class, new ClonesArguments());
43         when(emailSender.getAllEmails(new Person("Wes"))).thenAnswer(new ClonesArguments());
44 
45         // when
46         List<?> emails = emailSender.getAllEmails(new Person("Wes"));
47 
48         // then
49         assertNotNull(emails);
50     }
51 
52     @Test
shouldCloneArrays()53     public void shouldCloneArrays() throws Exception {
54 
55         EmailSender emailSender = mock(EmailSender.class, new ClonesArguments());
56 
57         // 1. Pass an array into a mock that "ClonesArguments"
58         Person[] ccList = new Person[] {new Person("Wes")};
59         emailSender.sendGroupEmail(1, ccList);
60 
61         // 2. Mutate the array
62         ccList[0] = new Person("Joe");
63 
64         // 3. Verify that the mock made a copy of the array
65         verify(emailSender).sendGroupEmail(1, new Person[] {new Person("Wes")});
66     }
67 
68     @Test
shouldNotThrowNPEWhenCloningNulls()69     public void shouldNotThrowNPEWhenCloningNulls() throws Exception {
70 
71         EmailSender emailSender = mock(EmailSender.class, new ClonesArguments());
72 
73         // 1. Pass a null into a mock that "ClonesArguments"
74         emailSender.sendEmail(1, (Person) null);
75 
76         // 2. Verify that the null argument was captured
77         verify(emailSender).sendEmail(eq(1), (Person) isNull());
78     }
79 
80     public class Person {
81 
82         private final String name;
83         private boolean emailSent;
84 
Person(String name)85         public Person(String name) {
86             this.name = name;
87         }
88 
emailSent()89         public void emailSent() {
90             emailSent = true;
91         }
92 
93         @Override
hashCode()94         public int hashCode() {
95             final int prime = 31;
96             int result = 1;
97             result = prime * result + getOuterType().hashCode();
98             result = prime * result + (emailSent ? 1231 : 1237);
99             result = prime * result + ((name == null) ? 0 : name.hashCode());
100             return result;
101         }
102 
103         @Override
equals(Object obj)104         public boolean equals(Object obj) {
105             if (this == obj) return true;
106             if (obj == null) return false;
107             if (getClass() != obj.getClass()) return false;
108             Person other = (Person) obj;
109             if (!getOuterType().equals(other.getOuterType())) return false;
110             if (emailSent != other.emailSent) return false;
111             if (name == null) {
112                 if (other.name != null) return false;
113             } else if (!name.equals(other.name)) return false;
114             return true;
115         }
116 
getOuterType()117         private CloningParameterTest getOuterType() {
118             return CloningParameterTest.this;
119         }
120     }
121 
122     public interface EmailSender {
123 
sendEmail(int i, Person person)124         void sendEmail(int i, Person person);
125 
sendGroupEmail(int i, Person[] persons)126         void sendGroupEmail(int i, Person[] persons);
127 
getAllEmails(Person person)128         List<?> getAllEmails(Person person);
129     }
130 }
131