Modernize testutils tests
[yangtools.git] / common / testutils / src / test / java / org / opendaylight / yangtools / testutils / mockito / tests / MikitoTest.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.assertThrows;
12 import static org.mockito.Mockito.mock;
13 import static org.opendaylight.yangtools.testutils.mockito.MoreAnswers.realOrException;
14
15 import java.io.File;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.testutils.mockito.UnstubbedMethodException;
18
19 /**
20  * Test to illustrate the use of the REAL_OR_EXCEPTION.
21  *
22  * <p>Also useful as example to contrast this approach illustrated in the MockitoExampleTutorialTest.
23  *
24  * @see MockitoExampleTutorialTest
25  *
26  * @author Michael Vorburger
27  */
28 public class MikitoTest {
29
30     interface SomeService {
31
32         void foo();
33
34         String bar(String arg);
35
36         // Most methods on real world services have complex input (and output objects), not just int or String
37         int foobar(File file);
38     }
39
40     @Test
41     public void usingMikitoToCallStubbedMethod() {
42         SomeService service = mock(MockSomeService.class, realOrException());
43         assertEquals(123, service.foobar(new File("hello.txt")));
44         assertEquals(0, service.foobar(new File("belo.txt")));
45     }
46
47     @Test
48     public void usingMikitoToCallUnstubbedMethodAndExpectException() {
49         MockSomeService service = mock(MockSomeService.class, realOrException());
50         String message = assertThrows(UnstubbedMethodException.class, service::foo).getMessage();
51         assertEquals("foo() is not implemented in mockSomeService", message);
52     }
53
54     abstract static class MockSomeService implements SomeService {
55         @Override
56         public int foobar(final File file) {
57             return "hello.txt".equals(file.getName()) ? 123 : 0;
58         }
59     }
60 }