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.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;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.testutils.mockito.UnstubbedMethodException;
25 * Test to illustrate the basic use of Mockito VS the EXCEPTION_ANSWER.
27 * <p>Also useful as example to contrast this approach with the REAL_OR_EXCEPTION
28 * approach illustrated in the MikitoTest.
32 * @author Michael Vorburger
34 public class MockitoExampleTutorialTest {
36 interface SomeService {
40 String bar(String arg);
42 // Most methods on real world services have complex input (and output objects), not just int or String
43 int foobar(File file);
47 public void usingMockitoWithoutStubbing() {
48 SomeService service = mock(SomeService.class);
49 assertNull(service.bar("hulo"));
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")));
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")) {
71 assertEquals(0, service.foobar(new File("belo.txt")));
74 @Test(expected = UnstubbedMethodException.class)
75 public void usingMockitoExceptionException() {
76 SomeService service = mock(SomeService.class, exception());
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")));
88 fail("expected NotImplementedException");
89 } catch (UnstubbedMethodException e) {
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")) {
105 }).when(service).foobar(any());
106 assertEquals(123, service.foobar(new File("hello.txt")));
107 assertEquals(0, service.foobar(new File("belo.txt")));