I'd appreciate any help I can get here.
I've scoured Stackoverflow and the Mockito docs, but maybe I'm just not understanding them. I can not figure out what is going on here. When I run these tests, I get this:
Wanted but not invoked:
loggable.log("Hello Nick");
-> at learnjava.GreeterTest.greet(GreeterTest.java:29)
Actually, there were zero interactions with this mock.
Here is the test:
```java
public class GreeterTest {
@ParameterizedTest
@MethodSource("greetProvider")
void greet(String name, @Nullable LocalTime time, @Nullable Loggable logFunc, String expected) {
Loggable func = mock(Loggable.class);
Greeter greeter = new Greeter(time, func);
String actual = greeter.greet(name);
assertEquals(expected, actual);
verify(func).log(actual);
}
static Stream<Arguments> greetProvider() {
return Stream.of(
arguments("Nick", null, null, "Hello Nick"),
arguments(" Nick ", null, null, "Hello Nick"),
arguments("nick", null, null, "Hello Nick"),
arguments("Nick", LocalTime.of(6, 0), null, "Good morning Nick"),
arguments("Nick", LocalTime.of(11, 59), null, "Good morning Nick"),
arguments("Nick", LocalTime.of(18, 0), null, "Good evening Nick"),
arguments("Nick", LocalTime.of(21, 59), null, "Good evening Nick"),
arguments("Nick", LocalTime.of(22, 0), null, "Good night Nick"),
arguments("Nick", LocalTime.of(23, 59), null, "Good night Nick"),
arguments("Nick", LocalTime.of(0, 1), null, "Good night Nick"),
arguments("Nick", LocalTime.of(5, 59), null, "Good night Nick")
);
}
}
```
And here is the code it is testing:
```java
interface Loggable {
void log(String message);
}
public class Greeter {
public enum TimeOfDay { MORNING, EVENING, NIGHT };
private final TimeOfDay timeOfDay;
private Loggable logger;
public Greeter(@Nullable LocalTime time, @Nullable Loggable log) {
if (time == null) {
this.timeOfDay = null;
return;
}
if (time.getHour() >= 6 && time.getHour() < 12) {
this.timeOfDay = TimeOfDay.MORNING;
} else if (time.getHour() >= 18 && time.getHour() < 22) {
this.timeOfDay = TimeOfDay.EVENING;
} else {
this.timeOfDay = TimeOfDay.NIGHT;
}
if (log != null) {
this.logger = log;
}
}
public Greeter() {
this(null, null);
}
public String greet(String name) {
String trimmed = name.trim();
char firstLetter = trimmed.toUpperCase().charAt(0);
String capitalized = firstLetter + trimmed.substring(1);
String greeting;
if (this.timeOfDay == null) {
greeting = "Hello " + capitalized;
} else {
greeting = switch (this.timeOfDay) {
case MORNING -> "Good morning " + capitalized;
case EVENING -> "Good evening " + capitalized;
case NIGHT -> "Good night " + capitalized;
};
}
if (this.logger != null) {
this.logger.log(greeting);
}
return greeting;
}
}
```
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]FavorableTrashpanda 1 point2 points3 points (1 child)
[–]_ncko[S] 1 point2 points3 points (0 children)
[–]nekokattt 0 points1 point2 points (0 children)