2 * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.testutils.mockito.tests;
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;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.testutils.mockito.UnstubbedMethodException;
20 * Test to illustrate the use of the REAL_OR_EXCEPTION.
22 * <p>Also useful as example to contrast this approach illustrated in the MockitoExampleTutorialTest.
24 * @see MockitoExampleTutorialTest
26 * @author Michael Vorburger
28 public class MikitoTest {
30 interface SomeService {
34 String bar(String arg);
36 // Most methods on real world services have complex input (and output objects), not just int or String
37 int foobar(File file);
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")));
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);
54 abstract static class MockSomeService implements SomeService {
56 public int foobar(final File file) {
57 return "hello.txt".equals(file.getName()) ? 123 : 0;