* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-package org.opendaylight.yangtools.testutils.mockito.tests;
+package org.opendaylight.yangtools.testutils.mockito;
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import java.lang.reflect.Method;
-import org.junit.Test;
-import org.opendaylight.yangtools.testutils.mockito.MethodExtensions;
+import org.junit.jupiter.api.Test;
public class MethodExtensionsTest {
- public <T> void fooBar(int index, T element) {
+ public <T> void fooBar(final int index, final T element) {
// No-op
}
@Test
- public void betterToString() throws Exception {
+ void betterToString() throws Exception {
Method method = MethodExtensionsTest.class.getMethod("fooBar", Integer.TYPE, Object.class);
assertEquals("fooBar(int index, T element)", MethodExtensions.toString(method));
}
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-package org.opendaylight.yangtools.testutils.mockito.tests;
+package org.opendaylight.yangtools.testutils.mockito;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.opendaylight.yangtools.testutils.mockito.MoreAnswers.realOrException;
import java.io.File;
-import org.junit.Test;
-import org.opendaylight.yangtools.testutils.mockito.UnstubbedMethodException;
+import org.junit.jupiter.api.Test;
/**
- * Test to illustrate the use of the REAL_OR_EXCEPTION.
- *
- * <p>Also useful as example to contrast this approach illustrated in the MockitoExampleTutorialTest.
+ * Test to illustrate the use of the REAL_OR_EXCEPTION. Also useful as example to contrast this approach illustrated in
+ * the MockitoExampleTutorialTest.
*
* @see MockitoExampleTutorialTest
- *
* @author Michael Vorburger
*/
-public class MikitoTest {
+class MikitoTest {
interface SomeService {
}
@Test
- public void usingMikitoToCallStubbedMethod() {
+ void usingMikitoToCallStubbedMethod() {
SomeService service = mock(MockSomeService.class, realOrException());
assertEquals(123, service.foobar(new File("hello.txt")));
assertEquals(0, service.foobar(new File("belo.txt")));
}
@Test
- public void usingMikitoToCallUnstubbedMethodAndExpectException() {
- MockSomeService service = mock(MockSomeService.class, realOrException());
- String message = assertThrows(UnstubbedMethodException.class, service::foo).getMessage();
- assertEquals("foo() is not implemented in mockSomeService", message);
+ void usingMikitoToCallUnstubbedMethodAndExpectException() {
+ var service = mock(MockSomeService.class, realOrException());
+ var ex = assertThrows(UnstubbedMethodException.class, service::foo);
+ assertEquals("foo() is not implemented in mockSomeService", ex.getMessage());
}
abstract static class MockSomeService implements SomeService {
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-package org.opendaylight.yangtools.testutils.mockito.tests;
+package org.opendaylight.yangtools.testutils.mockito;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.opendaylight.yangtools.testutils.mockito.MoreAnswers.exception;
import java.io.File;
-import org.junit.Test;
-import org.opendaylight.yangtools.testutils.mockito.UnstubbedMethodException;
+import org.junit.jupiter.api.Test;
/**
- * Test to illustrate the basic use of Mockito VS the EXCEPTION_ANSWER.
- *
- * <p>Also useful as example to contrast this approach with the REAL_OR_EXCEPTION
- * approach illustrated in the MikitoTest.
+ * Test to illustrate the basic use of Mockito VS the EXCEPTION_ANSWER. Also useful as example to contrast this approach
+ * with the REAL_OR_EXCEPTION approach illustrated in the MikitoTest.
*
* @see MikitoTest
- *
* @author Michael Vorburger
*/
-public class MockitoExampleTutorialTest {
+class MockitoExampleTutorialTest {
interface SomeService {
}
@Test
- public void usingMockitoWithoutStubbing() {
+ void usingMockitoWithoutStubbing() {
SomeService service = mock(SomeService.class);
assertNull(service.bar("hulo"));
}
@Test
- public void usingMockitoToStubSimpleCase() {
+ void usingMockitoToStubSimpleCase() {
SomeService service = mock(SomeService.class);
when(service.foobar(any())).thenReturn(123);
assertEquals(123, service.foobar(new File("hello.txt")));
}
@Test
- public void usingMockitoToStubComplexCase() {
+ void usingMockitoToStubComplexCase() {
SomeService service = mock(SomeService.class);
when(service.foobar(any())).thenAnswer(invocation -> {
File file = invocation.getArgument(0);
assertEquals(0, service.foobar(new File("belo.txt")));
}
- @Test(expected = UnstubbedMethodException.class)
- public void usingMockitoExceptionException() {
- SomeService service = mock(SomeService.class, exception());
- service.foo();
+ @Test
+ void usingMockitoExceptionException() {
+ assertThrows(UnstubbedMethodException.class, () -> {
+ SomeService service = mock(SomeService.class, exception());
+ service.foo();
+ });
}
@Test
- public void usingMockitoNoExceptionIfStubbed() {
+ void usingMockitoNoExceptionIfStubbed() {
SomeService service = mock(SomeService.class, exception());
// NOT when(s.foobar(any())).thenReturn(123) BUT must be like this:
doReturn(123).when(service).foobar(any());
}
@Test
- public void usingMockitoToStubComplexCaseAndExceptionIfNotStubbed() {
+ void usingMockitoToStubComplexCaseAndExceptionIfNotStubbed() {
SomeService service = mock(SomeService.class, exception());
doAnswer(invocation -> {
File file = invocation.getArgument(0);
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-package org.opendaylight.yangtools.testutils.mockito.tests;
+package org.opendaylight.yangtools.testutils.mockito;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThrows;
-import static org.opendaylight.yangtools.testutils.mockito.MoreAnswers.exception;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.Closeable;
import java.io.IOException;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
-import org.opendaylight.yangtools.testutils.mockito.UnstubbedMethodException;
-public class MockitoUnstubbedMethodExceptionAnswerTest {
+class MockitoUnstubbedMethodExceptionAnswerTest {
@Test
- public void testAnswering() throws IOException {
- Closeable mock = Mockito.mock(Closeable.class, exception());
+ void testAnswering() throws IOException {
+ Closeable mock = Mockito.mock(Closeable.class, MoreAnswers.exception());
String message = assertThrows(UnstubbedMethodException.class, mock::close).getMessage();
assertEquals("close() is not stubbed in mock of java.io.Closeable", message);
}