Issue 497 in mockito: Mock returning java.util.Optional should return Optional.empty() by default (Java 8)

1,615 views
Skip to first unread message

moc...@googlecode.com

unread,
Jul 8, 2014, 4:44:45 AM7/8/14
to mocki...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 497 by phjar...@gmail.com: Mock returning java.util.Optional
should return Optional.empty() by default (Java 8)
http://code.google.com/p/mockito/issues/detail?id=497

This issue is closely related to #349, however I am referring to the now
standard type java.util.Optional in Java 8 instead of Guava's.

A stubbed method that returns Optional<?> should return Optional.empty() if
no explicit behavior is defined. This is equivalent to returning null for
regular methods.

Introducing this feature would add no extra dependencies. However, it would
require a test whether the code is running in Java 8.

--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

moc...@googlecode.com

unread,
Jul 8, 2014, 5:13:09 AM7/8/14
to mocki...@googlegroups.com
Updates:
Labels: jdk8

Comment #1 on issue 497 by brice.du...@gmail.com: Mock returning
java.util.Optional should return Optional.empty() by default (Java 8)
http://code.google.com/p/mockito/issues/detail?id=497

That would be great, but for now our build has to be compatible with Java
5. So we cannot yet use Java 6,7 or 8 symbols or types.

However a specific jdk8 distribution of mockito is clearly not ruled out.

moc...@googlecode.com

unread,
Jul 8, 2014, 5:35:27 AM7/8/14
to mocki...@googlegroups.com

Comment #2 on issue 497 by phjar...@gmail.com: Mock returning
java.util.Optional should return Optional.empty() by default (Java 8)
http://code.google.com/p/mockito/issues/detail?id=497

Why not dynamically include modules if a required Java version (or any
other library) is present?

The Spring Framework does something like this in many places, mainly to
load plugins depending on the presence of a certain class in the class
path. See
https://github.com/spring-projects/spring-framework/blob/master/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java#L130

Demo code that loads a class only if the Java version meets a certain
requirement follows (with console output and simplified error handling to
keep things simple).

public final class DynamicImport {
private static final int JAVA_VERSION;

static {
final String versionString = System.getProperty("java.version");
final Matcher matcher =
Pattern.compile("^1\\.(\\d+)\\.\\d+_\\d+$").matcher(versionString);

if (!matcher.matches()) {
throw new IllegalStateException(String.format("Can't parse Java version
[%s]", versionString));
}

JAVA_VERSION = Integer.parseInt(matcher.group(1));
}

private DynamicImport() {
// utility class
}

public static void includeClass(final int minVersion, final String
className, final ClassLoader loader) {
if (JAVA_VERSION < minVersion) {
System.out.println(String.format("Skipping loading of class [%s]: Java
version %d is lower than required %d", className,
JAVA_VERSION, minVersion));
return;
}

try {
System.out.println(String.format("Loading class [%s]", className));
Class.forName(className, true, loader);
} catch (final ClassNotFoundException e) {
throw new IllegalStateException(String.format("Error loading class
[%s]: %s", className, e), e);

moc...@googlecode.com

unread,
Jul 8, 2014, 6:29:19 AM7/8/14
to mocki...@googlegroups.com

Comment #3 on issue 497 by brice.du...@gmail.com: Mock returning
java.util.Optional should return Optional.empty() by default (Java 8)
http://code.google.com/p/mockito/issues/detail?id=497

Yeah I know this is possible, we do something similar for JUnit. However as
it involves the JDK it requires to change the build scripts, the dev
environment, the ci, etc.

These tasks may be more relevant if we create a pure jdk8 version.

moc...@googlecode.com

unread,
Jul 8, 2014, 6:35:50 AM7/8/14
to mocki...@googlegroups.com

Comment #4 on issue 497 by phjar...@gmail.com: Mock returning
java.util.Optional should return Optional.empty() by default (Java 8)
http://code.google.com/p/mockito/issues/detail?id=497

Agreed.

moc...@googlecode.com

unread,
Dec 2, 2014, 3:47:20 AM12/2/14
to mocki...@googlegroups.com

Comment #5 on issue 497 by jdb...@diversit.eu: Mock returning
java.util.Optional should return Optional.empty() by default (Java 8)
https://code.google.com/p/mockito/issues/detail?id=497

It would be nice if Mockito would support this by default, but ... it is
very easy to add this yourself using a default answer. I have put an
example here: https://gist.github.com/diversit/ef6a1025ac9d1dc2a113

moc...@googlecode.com

unread,
Dec 2, 2014, 4:28:28 AM12/2/14
to mocki...@googlegroups.com

Comment #6 on issue 497 by szcze...@gmail.com: Mock returning
java.util.Optional should return Optional.empty() by default (Java 8)
https://code.google.com/p/mockito/issues/detail?id=497

+1 for the feature

Do you mind opening a ticket in github?
https://github.com/mockito/mockito/issues

We can use reflection to avoid compiling against jdk8

moc...@googlecode.com

unread,
Apr 2, 2015, 2:04:41 AM4/2/15
to mocki...@googlegroups.com

Comment #7 on issue 497 by netmi...@gmail.com: Mock returning
java.util.Optional should return Optional.empty() by default (Java 8)
https://code.google.com/p/mockito/issues/detail?id=497

If you reach this via Google: I created a GitHub issue since I couldn't
find one: https://github.com/mockito/mockito/issues/191
Reply all
Reply to author
Forward
0 new messages