Cleanup: remove redundant casts
[yangtools.git] / common / testutils / src / test / java / org / opendaylight / yangtools / testutils / mockito / tests / MockitoExampleTutorialTest.java
1 /*
2  * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.testutils.mockito.tests;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12 import static org.junit.Assert.fail;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.doAnswer;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.when;
18 import static org.opendaylight.yangtools.testutils.mockito.MoreAnswers.exception;
19
20 import java.io.File;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.testutils.mockito.UnstubbedMethodException;
23
24 /**
25  * Test to illustrate the basic use of Mockito VS the EXCEPTION_ANSWER.
26  *
27  * <p>Also useful as example to contrast this approach with the REAL_OR_EXCEPTION
28  * approach illustrated in the MikitoTest.
29  *
30  * @see MikitoTest
31  *
32  * @author Michael Vorburger
33  */
34 public class MockitoExampleTutorialTest {
35
36     interface SomeService {
37
38         void foo();
39
40         String bar(String arg);
41
42         // Most methods on real world services have complex input (and output objects), not just int or String
43         int foobar(File file);
44     }
45
46     @Test
47     public void usingMockitoWithoutStubbing() {
48         SomeService service = mock(SomeService.class);
49         assertNull(service.bar("hulo"));
50     }
51
52     @Test
53     public void usingMockitoToStubSimpleCase() {
54         SomeService service = mock(SomeService.class);
55         when(service.foobar(any())).thenReturn(123);
56         assertEquals(123, service.foobar(new File("hello.txt")));
57     }
58
59     @Test
60     public void usingMockitoToStubComplexCase() {
61         SomeService service = mock(SomeService.class);
62         when(service.foobar(any())).thenAnswer(invocation -> {
63             // Urgh! This is ugly.. (Mockito 2.0 may be better, see http://site.mockito.org/mockito/docs/current/org/mockito/ArgumentMatcher.html)
64             File file = invocation.getArgumentAt(0, File.class);
65             if (file.getName().equals("hello.txt")) {
66                 return 123;
67             } else {
68                 return 0;
69             }
70         });
71         assertEquals(0, service.foobar(new File("belo.txt")));
72     }
73
74     @Test(expected = UnstubbedMethodException.class)
75     public void usingMockitoExceptionException() {
76         SomeService service = mock(SomeService.class, exception());
77         service.foo();
78     }
79
80     @Test
81     public void usingMockitoNoExceptionIfStubbed() {
82         SomeService service = mock(SomeService.class, exception());
83         // NOT when(s.foobar(any())).thenReturn(123) BUT must be like this:
84         doReturn(123).when(service).foobar(any());
85         assertEquals(123, service.foobar(new File("hello.txt")));
86         try {
87             service.foo();
88             fail("expected NotImplementedException");
89         } catch (UnstubbedMethodException e) {
90             // OK
91         }
92     }
93
94     @Test
95     public void usingMockitoToStubComplexCaseAndExceptionIfNotStubbed() {
96         SomeService service = mock(SomeService.class, exception());
97         doAnswer(invocation -> {
98             // Urgh! This is ugly. Mockito may be better, see http://site.mockito.org/mockito/docs/current/org/mockito/ArgumentMatcher.html
99             File file = (File) invocation.getArguments()[0];
100             if (file.getName().equals("hello.txt")) {
101                 return 123;
102             } else {
103                 return 0;
104             }
105         }).when(service).foobar(any());
106         assertEquals(123, service.foobar(new File("hello.txt")));
107         assertEquals(0, service.foobar(new File("belo.txt")));
108     }
109
110 }