From ce007163629dab86f15184793b20b56c295922a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20K=C3=BCper?= Date: Sun, 11 Apr 2021 11:48:49 +0200 Subject: [PATCH] 10386: @NonNullByDefault and Mockito don't like each other MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Sönke Küper --- .../AhaWasteCollectionHandlerTest.java | 100 ++++++++---------- 1 file changed, 47 insertions(+), 53 deletions(-) diff --git a/bundles/org.openhab.binding.ahawastecollection/src/test/java/org/openhab/binding/ahawastecollection/internal/AhaWasteCollectionHandlerTest.java b/bundles/org.openhab.binding.ahawastecollection/src/test/java/org/openhab/binding/ahawastecollection/internal/AhaWasteCollectionHandlerTest.java index b3ed481ad133d..6c48b04eb2862 100644 --- a/bundles/org.openhab.binding.ahawastecollection/src/test/java/org/openhab/binding/ahawastecollection/internal/AhaWasteCollectionHandlerTest.java +++ b/bundles/org.openhab.binding.ahawastecollection/src/test/java/org/openhab/binding/ahawastecollection/internal/AhaWasteCollectionHandlerTest.java @@ -22,9 +22,6 @@ import java.util.Map; import org.eclipse.jdt.annotation.NonNullByDefault; -import org.eclipse.jdt.annotation.Nullable; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.openhab.core.config.core.Configuration; @@ -38,7 +35,6 @@ import org.openhab.core.thing.Thing; import org.openhab.core.thing.ThingStatus; import org.openhab.core.thing.ThingUID; -import org.openhab.core.thing.binding.ThingHandler; import org.openhab.core.thing.binding.ThingHandlerCallback; import org.openhab.core.types.State; @@ -48,28 +44,28 @@ @NonNullByDefault public class AhaWasteCollectionHandlerTest { - private @Nullable ThingHandler handler; - private @Nullable ThingHandlerCallback callback; - private @Nullable Thing thing; + private static final Configuration config = createConfig(); - @BeforeEach - public void setUp() { + private static Configuration createConfig() { final Configuration config = new Configuration(); config.put("commune", "Hannover"); config.put("collectionPlace", "02095-0010+"); config.put("houseNumber", "10"); config.put("houseNumberAddon", ""); config.put("street", "02095@Oesterleystr.+/+Südstadt@Südstadt"); + return config; + } - this.thing = mockThing(config); - - // Stub-Scheduler that executes the command synchronous - @SuppressWarnings("unchecked") - final CronScheduler scheduler = new CronScheduler() { + /** + * Creates an {@link CronScheduler} that executes all commands synchronous. + */ + @SuppressWarnings("unchecked") + private static CronScheduler createStubScheduler() { + return new CronScheduler() { @Override - public ScheduledCompletableFuture schedule(final CronJob cronJob, final Map config, - final String cronExpression) { + public final ScheduledCompletableFuture schedule(final CronJob cronJob, + final Map config, final String cronExpression) { try { cronJob.run(config); } catch (final Exception e) { @@ -79,7 +75,7 @@ public ScheduledCompletableFuture schedule(final CronJob cronJob, final Ma } @Override - public ScheduledCompletableFuture schedule(final SchedulerRunnable runnable, + public final ScheduledCompletableFuture schedule(final SchedulerRunnable runnable, final String cronExpression) { try { runnable.run(); @@ -89,11 +85,6 @@ public ScheduledCompletableFuture schedule(final SchedulerRunnable runnabl return Mockito.mock(ScheduledCompletableFuture.class); } }; - - this.handler = new AhaWasteCollectionHandler(this.getThing(), scheduler, ZoneId::systemDefault, - new AhaCollectionScheduleStubFactory()); - this.callback = mock(ThingHandlerCallback.class); - this.handler.setCallback(this.callback); } private static Thing mockThing(final Configuration config) { @@ -120,43 +111,46 @@ private static Channel mockChannel(final ThingUID thingId, final String channelI return channel; } - // Needed because of NotNullCheck will fail otherwise when using attribute thing in mockito verify. - private Thing getThing() { - if (this.thing != null) { - return this.thing; - } else { - throw new AssertionError(); - } + private static AhaWasteCollectionHandler createAndInitHandler(final ThingHandlerCallback callback, + final Thing thing) { + final AhaWasteCollectionHandler handler = new AhaWasteCollectionHandler(thing, createStubScheduler(), + ZoneId::systemDefault, new AhaCollectionScheduleStubFactory()); + + handler.setCallback(callback); + handler.initialize(); + return handler; } - @AfterEach - public void tearDown() { - this.handler.dispose(); + private static State getDateTime(final Date day) { + final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(day.toInstant(), ZoneId.systemDefault()); + return new DateTimeType(zonedDateTime); } @Test public void testUpdateChannels() { - this.handler.initialize(); - verify(this.callback).statusUpdated(eq(this.getThing()), - argThat(arg -> arg.getStatus().equals(ThingStatus.UNKNOWN))); - verify(this.callback, timeout(1000)).statusUpdated(eq(this.thing), - argThat(arg -> arg.getStatus().equals(ThingStatus.ONLINE))); - verify(this.callback, timeout(1000)).stateUpdated( - new ChannelUID(this.thing.getUID(), AhaWasteCollectionBindingConstants.BIOWASTE), - getDateTime(AhaCollectionScheduleStub.BIO_WASTE_DATE)); - verify(this.callback, timeout(1000)).stateUpdated( - new ChannelUID(this.thing.getUID(), AhaWasteCollectionBindingConstants.GENERAL_WASTE), - getDateTime(AhaCollectionScheduleStub.GENERAL_WASTE_DATE)); - verify(this.callback, timeout(1000)).stateUpdated( - new ChannelUID(this.thing.getUID(), AhaWasteCollectionBindingConstants.LEIGHTWEIGHT_PACKAGING), - getDateTime(AhaCollectionScheduleStub.LEIGHTWEIGHT_PACKAGING_DATE)); - verify(this.callback, timeout(1000)).stateUpdated( - new ChannelUID(this.thing.getUID(), AhaWasteCollectionBindingConstants.PAPER), - getDateTime(AhaCollectionScheduleStub.PAPER_DATE)); - } - private static State getDateTime(final Date day) { - final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(day.toInstant(), ZoneId.systemDefault()); - return new DateTimeType(zonedDateTime); + final Thing thing = mockThing(config); + final ThingHandlerCallback callback = mock(ThingHandlerCallback.class); + final AhaWasteCollectionHandler handler = createAndInitHandler(callback, thing); + + try { + verify(callback).statusUpdated(eq(thing), argThat(arg -> arg.getStatus().equals(ThingStatus.UNKNOWN))); + verify(callback, timeout(1000)).statusUpdated(eq(thing), + argThat(arg -> arg.getStatus().equals(ThingStatus.ONLINE))); + verify(callback, timeout(1000)).stateUpdated( + new ChannelUID(thing.getUID(), AhaWasteCollectionBindingConstants.BIOWASTE), + getDateTime(AhaCollectionScheduleStub.BIO_WASTE_DATE)); + verify(callback, timeout(1000)).stateUpdated( + new ChannelUID(thing.getUID(), AhaWasteCollectionBindingConstants.GENERAL_WASTE), + getDateTime(AhaCollectionScheduleStub.GENERAL_WASTE_DATE)); + verify(callback, timeout(1000)).stateUpdated( + new ChannelUID(thing.getUID(), AhaWasteCollectionBindingConstants.LEIGHTWEIGHT_PACKAGING), + getDateTime(AhaCollectionScheduleStub.LEIGHTWEIGHT_PACKAGING_DATE)); + verify(callback, timeout(1000)).stateUpdated( + new ChannelUID(thing.getUID(), AhaWasteCollectionBindingConstants.PAPER), + getDateTime(AhaCollectionScheduleStub.PAPER_DATE)); + } finally { + handler.dispose(); + } } }