Cleans up sonar warnings and re-organizes code a bit.
Change-Id: I99761f484cd992d994f1bca7c681190a055793cd
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
@Beta
public class CallsRealOrExceptionAnswer implements Answer<Object>, Serializable {
private static final long serialVersionUID = -3730024662402964588L;
+ static final CallsRealOrExceptionAnswer INSTANCE = new CallsRealOrExceptionAnswer();
+
+ private CallsRealOrExceptionAnswer() {
- /**
- * Use {@link MoreAnswers} to obtain an instance.
- */
- CallsRealOrExceptionAnswer() {
}
@Override
- public Object answer(InvocationOnMock invocation) throws Throwable {
+ public Object answer(final InvocationOnMock invocation) throws Throwable {
if (Modifier.isAbstract(invocation.getMethod().getModifiers())) {
throw new UnstubbedMethodException(invocation.getMethod(), invocation.getMock());
}
return invocation.callRealMethod();
}
+
+ Object readResolve() {
+ return INSTANCE;
+ }
}
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.Type;
+import java.util.regex.Pattern;
+import javax.annotation.RegEx;
/**
* Nicer shorter toString() for {@link Method} than it's default.
* @author Michael Vorburger
*/
public final class MethodExtensions {
+ @RegEx
+ private static final String PARAM_PATTERN_STR = "\\[\\]$";
+ private static final Pattern PARAM_PATTERN = Pattern.compile(PARAM_PATTERN_STR);
private MethodExtensions() {
}
- public static String toString(Method method) {
- StringBuilder sb = new StringBuilder();
+ public static String toString(final Method method) {
+ final StringBuilder sb = new StringBuilder();
sb.append(method.getName());
// copy/paste from java.lang.reflect.Executable.sharedToGenericString(int, boolean)
sb.append('(');
- Type[] params = method.getGenericParameterTypes();
- Parameter[] parameters = method.getParameters(); // NEW
+ final Type[] params = method.getGenericParameterTypes();
+ // NEW
+ final Parameter[] parameters = method.getParameters();
for (int j = 0; j < params.length; j++) {
String param = params[j].getTypeName();
- if (method.isVarArgs() && j == params.length - 1) { // replace T[] with T...
- param = param.replaceFirst("\\[\\]$", "...");
+ if (method.isVarArgs() && j == params.length - 1) {
+ // replace T[] with T...
+ param = PARAM_PATTERN.matcher(param).replaceFirst("...");
}
sb.append(param);
// NEW
sb.append(parameters[j].getName());
}
// NEW END
- if (j < (params.length - 1)) {
- sb.append(", "); // NEW ", " instead of ','
+ if (j < params.length - 1) {
+ // NEW ", " instead of ','
+ sb.append(", ");
}
}
sb.append(')');
*/
@SuppressWarnings("unchecked")
public final class MoreAnswers {
-
- private static final CallsRealOrExceptionAnswer REAL_OR_EXCEPTION
- = new CallsRealOrExceptionAnswer();
-
- private static final ThrowsMethodExceptionAnswer EXCEPTION
- = new ThrowsMethodExceptionAnswer();
-
private MoreAnswers() {
}
* @see CallsRealOrExceptionAnswer
*/
public static <T> Answer<T> realOrException() {
- return (Answer<T>) REAL_OR_EXCEPTION;
+ return (Answer<T>) CallsRealOrExceptionAnswer.INSTANCE;
}
/**
* @see ThrowsMethodExceptionAnswer
*/
public static <T> Answer<T> exception() {
- return (Answer<T>) EXCEPTION;
+ return (Answer<T>) ThrowsMethodExceptionAnswer.INSTANCE;
}
-
}
@Beta
public class ThrowsMethodExceptionAnswer implements Answer<Object>, Serializable {
private static final long serialVersionUID = -7316574192253912318L;
+ static final ThrowsMethodExceptionAnswer INSTANCE = new ThrowsMethodExceptionAnswer();
+
+ private ThrowsMethodExceptionAnswer() {
- /**
- * Use {@link MoreAnswers} to obtain an instance.
- */
- ThrowsMethodExceptionAnswer() {
}
@Override
- public Void answer(InvocationOnMock invocation) throws Throwable {
+ public Void answer(final InvocationOnMock invocation) throws Throwable {
throw new UnstubbedMethodException(invocation.getMethod());
}
+ Object readResolve() {
+ return INSTANCE;
+ }
}
public class UnstubbedMethodException extends UnsupportedOperationException {
private static final long serialVersionUID = 1L;
- public UnstubbedMethodException(Method method) {
+ public UnstubbedMethodException(final Method method) {
super(MethodExtensions.toString(method) + " is not stubbed in mock of " + method.getDeclaringClass().getName());
}
- public UnstubbedMethodException(Method method, Object mockAbstractFakeObject) {
+ public UnstubbedMethodException(final Method method, final Object mockAbstractFakeObject) {
super(MethodExtensions.toString(method) + " is not implemented in "
+ new MockUtil().getMockName(mockAbstractFakeObject).toString());
}
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
+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.mockito.Mockito;
import org.opendaylight.yangtools.testutils.mockito.UnstubbedMethodException;
/**
@Test
public void usingMikitoToCallStubbedMethod() {
- SomeService service = Mockito.mock(MockSomeService.class, realOrException());
+ 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 = Mockito.mock(MockSomeService.class, realOrException());
+ MockSomeService service = mock(MockSomeService.class, realOrException());
try {
service.foo();
fail();
abstract static class MockSomeService implements SomeService {
@Override
- public int foobar(File file) {
- if (file.getName().equals("hello.txt")) {
- return 123;
- } else {
- return 0;
- }
+ public int foobar(final File file) {
+ return "hello.txt".equals(file.getName()) ? 123 : 0;
}
}
-
}
public void usingMockitoToStubComplexCase() {
SomeService service = mock(SomeService.class);
when(service.foobar(any())).thenAnswer(invocation -> {
- // Urgh! This is ugly.. (Mockito 2.0 may be better, see http://site.mockito.org/mockito/docs/current/org/mockito/ArgumentMatcher.html)
+ // Urgh! This is ugly.. (Mockito 2.0 may be better,
+ // see http://site.mockito.org/mockito/docs/current/org/mockito/ArgumentMatcher.html)
File file = invocation.getArgumentAt(0, File.class);
- if (file.getName().equals("hello.txt")) {
- return 123;
- } else {
- return 0;
- }
+ return "hello.txt".equals(file.getName()) ? 123 : 0;
});
assertEquals(0, service.foobar(new File("belo.txt")));
}
public void usingMockitoToStubComplexCaseAndExceptionIfNotStubbed() {
SomeService service = mock(SomeService.class, exception());
doAnswer(invocation -> {
- // Urgh! This is ugly. Mockito may be better, see http://site.mockito.org/mockito/docs/current/org/mockito/ArgumentMatcher.html
+ // Urgh! This is ugly. Mockito may be better,
+ // see http://site.mockito.org/mockito/docs/current/org/mockito/ArgumentMatcher.html
File file = (File) invocation.getArguments()[0];
- if (file.getName().equals("hello.txt")) {
- return 123;
- } else {
- return 0;
- }
+ return "hello.txt".equals(file.getName()) ? 123 : 0;
}).when(service).foobar(any());
assertEquals(123, service.foobar(new File("hello.txt")));
assertEquals(0, service.foobar(new File("belo.txt")));