<packaging>jar</packaging><!-- not needed in OSGi -->
<name>${project.artifactId}</name>
+ <properties>
+ <maven.javadoc.failOnWarnings>true</maven.javadoc.failOnWarnings>
+ </properties>
+
<dependencies>
<!-- all those dependencies will be in test scope as mockito-configuration should be referenced as test scope dependency -->
<dependency>
<artifactId>mockito-core</artifactId>
<scope>compile</scope>
</dependency>
- <dependency>
- <groupId>com.github.spotbugs</groupId>
- <artifactId>spotbugs-annotations</artifactId>
- <optional>true</optional>
- </dependency>
</dependencies>
</project>
package org.mockito.configuration;
import org.mockito.stubbing.Answer;
+import org.opendaylight.mockito.MoreAnswers;
/**
- * Configuration customization for Mockito. Change default answer to {@link ThrowsUnstubbedMethodException}.
+ * Configuration customization for Mockito. Change default answer to {@link MoreAnswers#THROWS_UNSTUBBED_METHOD}.
*/
public class MockitoConfiguration extends DefaultMockitoConfiguration {
@Override
public Answer<Object> getDefaultAnswer() {
- return new ThrowsUnstubbedMethodException();
+ return MoreAnswers.THROWS_UNSTUBBED_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.mockito.configuration;
+package org.opendaylight.mockito;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.invocation.InvocationsFinder;
--- /dev/null
+/*
+ * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.mockito;
+
+import static java.util.Objects.requireNonNull;
+
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+/**
+ * Additional {@link Answer}s.
+ */
+public enum MoreAnswers implements Answer<Object> {
+ /**
+ * An {@link Answer} that throws {@link UnstubbedMethodException}.
+ */
+ THROWS_UNSTUBBED_METHOD(new ThrowsUnstubbedMethod());
+
+ private final ThrowsUnstubbedMethod implementation;
+
+ MoreAnswers(final ThrowsUnstubbedMethod implementation) {
+ this.implementation = requireNonNull(implementation);
+ }
+
+ @Override
+ public Object answer(final InvocationOnMock invocation) throws Throwable {
+ return implementation.answer(invocation);
+ }
+}
* 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.mockito.configuration;
+package org.opendaylight.mockito;
-import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.Serializable;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
/**
- * Answer that throws {@link UnstubbedMethodException}.
+ * An {@link Answer} that throws {@link UnstubbedMethodException}.
*/
-@SuppressFBWarnings("NM_CLASS_NOT_EXCEPTION")
-public class ThrowsUnstubbedMethodException implements Answer<Object>, Serializable {
+final class ThrowsUnstubbedMethod implements Answer<Object>, Serializable {
+ @java.io.Serial
private static final long serialVersionUID = 1L;
@Override
public Object answer(final InvocationOnMock invocation) {
- throw new UnstubbedMethodException(invocation.toString() + " was not stubbed");
+ throw new UnstubbedMethodException(invocation + " was not stubbed");
}
}
* 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.mockito.configuration;
+package org.opendaylight.mockito;
+
+import static java.util.Objects.requireNonNull;
/**
* Exception to be thrown on unstubbed method call.
*/
-public class UnstubbedMethodException extends RuntimeException {
+public final class UnstubbedMethodException extends RuntimeException {
+ @java.io.Serial
private static final long serialVersionUID = 1L;
+ /**
+ * Default constructor.
+ *
+ * @param message detail message
+ */
public UnstubbedMethodException(final String message) {
- super(message);
+ super(requireNonNull(message));
}
}
--- /dev/null
+/*
+ * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+/**
+ * Various utilities for working with Mockito -- for example {@link org.opendaylight.mockito.MoreAnswers}.
+ */
+package org.opendaylight.mockito;
\ No newline at end of file
* 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.mockito.configuration;
+package org.opendaylight.mockito;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.mockito.ArgumentMatchers.any;
@ExtendWith(MockitoExtension.class)
class ArgumentsExtractorVerifierTest {
@Mock
- List<String> mockedList;
+ private List<String> mockedList;
@Test
void test() {
doReturn(Boolean.TRUE).when(mockedList).add(any(String.class));
- final String argument = "something";
+ final var argument = "something";
mockedList.add(argument);
// retrieve argument
- final ArgumentsExtractorVerifier argumentsExtractorVerifier = new ArgumentsExtractorVerifier();
+ final var argumentsExtractorVerifier = new ArgumentsExtractorVerifier();
verify(mockedList, argumentsExtractorVerifier).add(any(String.class));
- assertArrayEquals(new Object[]{argument}, argumentsExtractorVerifier.getArguments());
+ assertArrayEquals(new Object[]{ argument }, argumentsExtractorVerifier.getArguments());
}
}
* 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.mockito.configuration;
+package org.opendaylight.mockito;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.mockito.Mockito.mock;
import java.io.Closeable;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+@ExtendWith(MockitoExtension.class)
class DefaultAnswerTest {
+ @Mock
+ private Closeable mock;
+
@Test
void testAnswering() {
- final var mock = mock(Closeable.class);
final var e = assertThrows(UnstubbedMethodException.class, mock::close);
- assertEquals("closeable.close(); was not stubbed", e.getMessage());
+ assertEquals("mock.close(); was not stubbed", e.getMessage());
}
}