• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2018 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.mockito.Mockito.mock;
8 import static org.mockito.Mockito.withSettings;
9 
10 import org.assertj.core.api.Assertions;
11 import org.junit.Test;
12 import org.mockito.Answers;
13 
14 // Reproduces issue #1551
15 public class SmartNullsGenericBugTest {
16 
17     @Test
smart_nulls_generic_bug_generic_T()18     public void smart_nulls_generic_bug_generic_T() {
19         ConcreteDao concreteDao =
20                 mock(ConcreteDao.class, withSettings().defaultAnswer(Answers.RETURNS_SMART_NULLS));
21 
22         final Entity result = concreteDao.findById();
23 
24         Assertions.assertThat(result).as("#1551").isNotNull();
25     }
26 
27     @Test
smart_nulls_generic_bug_generic_M()28     public void smart_nulls_generic_bug_generic_M() {
29         ConcreteDao concreteDao =
30                 mock(ConcreteDao.class, withSettings().defaultAnswer(Answers.RETURNS_SMART_NULLS));
31 
32         final String other = concreteDao.find();
33 
34         Assertions.assertThat(other).as("#1551 - CCannot resolve type").isNull();
35     }
36 
37     @Test
smart_nulls_generic_bug_generic_M_provided_in_args()38     public void smart_nulls_generic_bug_generic_M_provided_in_args() {
39         ConcreteDao concreteDao =
40                 mock(ConcreteDao.class, withSettings().defaultAnswer(Answers.RETURNS_SMART_NULLS));
41 
42         final String other = concreteDao.findArgs(1, "plop");
43 
44         Assertions.assertThat(other).as("#1551").isEqualTo("");
45     }
46 
47     @Test
smart_nulls_generic_bug_generic_M_provided_as_varargs()48     public void smart_nulls_generic_bug_generic_M_provided_as_varargs() {
49         ConcreteDao concreteDao =
50                 mock(ConcreteDao.class, withSettings().defaultAnswer(Answers.RETURNS_SMART_NULLS));
51 
52         final String other = concreteDao.findVarargs(42, "plip", "plop");
53 
54         Assertions.assertThat(other).as("#1551").isEqualTo("");
55     }
56 
57     static class AbstractDao<T> {
findById()58         T findById() {
59             return null;
60         }
61 
find()62         <M> M find() {
63             return null;
64         }
65 
findArgs(int idx, M arg)66         <M> M findArgs(int idx, M arg) {
67             return null;
68         }
69 
findVarargs(int idx, M... args)70         <M> M findVarargs(int idx, M... args) {
71             return null;
72         }
73     }
74 
75     static class Entity {}
76 
77     static class ConcreteDao extends AbstractDao<Entity> {}
78 }
79