Migrate testutils to JUnit5
[yangtools.git] / common / testutils / src / test / java / org / opendaylight / yangtools / testutils / mockito / 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;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertNull;
12 import static org.junit.jupiter.api.Assertions.assertThrows;
13 import static org.mockito.ArgumentMatchers.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.jupiter.api.Test;
22
23 /**
24  * Test to illustrate the basic use of Mockito VS the EXCEPTION_ANSWER. Also useful as example to contrast this approach
25  * with the REAL_OR_EXCEPTION approach illustrated in the MikitoTest.
26  *
27  * @see MikitoTest
28  * @author Michael Vorburger
29  */
30 class MockitoExampleTutorialTest {
31
32     interface SomeService {
33
34         void foo();
35
36         String bar(String arg);
37
38         // Most methods on real world services have complex input (and output objects), not just int or String
39         int foobar(File file);
40     }
41
42     @Test
43     void usingMockitoWithoutStubbing() {
44         SomeService service = mock(SomeService.class);
45         assertNull(service.bar("hulo"));
46     }
47
48     @Test
49     void usingMockitoToStubSimpleCase() {
50         SomeService service = mock(SomeService.class);
51         when(service.foobar(any())).thenReturn(123);
52         assertEquals(123, service.foobar(new File("hello.txt")));
53     }
54
55     @Test
56     void usingMockitoToStubComplexCase() {
57         SomeService service = mock(SomeService.class);
58         when(service.foobar(any())).thenAnswer(invocation -> {
59             File file = invocation.getArgument(0);
60             return "hello.txt".equals(file.getName()) ? 123 : 0;
61         });
62         assertEquals(0, service.foobar(new File("belo.txt")));
63     }
64
65     @Test
66     void usingMockitoExceptionException() {
67         assertThrows(UnstubbedMethodException.class, () -> {
68             SomeService service = mock(SomeService.class, exception());
69             service.foo();
70         });
71     }
72
73     @Test
74     void usingMockitoNoExceptionIfStubbed() {
75         SomeService service = mock(SomeService.class, exception());
76         // NOT when(s.foobar(any())).thenReturn(123) BUT must be like this:
77         doReturn(123).when(service).foobar(any());
78         assertEquals(123, service.foobar(new File("hello.txt")));
79         assertThrows(UnstubbedMethodException.class, service::foo);
80     }
81
82     @Test
83     void usingMockitoToStubComplexCaseAndExceptionIfNotStubbed() {
84         SomeService service = mock(SomeService.class, exception());
85         doAnswer(invocation -> {
86             File file = invocation.getArgument(0);
87             return "hello.txt".equals(file.getName()) ? 123 : 0;
88         }).when(service).foobar(any());
89         assertEquals(123, service.foobar(new File("hello.txt")));
90         assertEquals(0, service.foobar(new File("belo.txt")));
91     }
92 }