Migrate tests to JUnit 5 (#8519)

Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
Wouter Born
2020-09-21 18:21:26 +02:00
committed by GitHub
parent 6df6783b60
commit bd82ca82df
478 changed files with 3996 additions and 4419 deletions

View File

@@ -13,11 +13,11 @@
package org.openhab.binding.mqtt.generic;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.initMocks;
import java.math.BigDecimal;
import java.time.ZonedDateTime;
@@ -31,11 +31,15 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.openhab.binding.mqtt.generic.mapping.ColorMode;
import org.openhab.binding.mqtt.generic.values.ColorValue;
import org.openhab.binding.mqtt.generic.values.DateTimeValue;
@@ -55,26 +59,21 @@ import org.openhab.core.thing.ChannelUID;
*
* @author David Graeff - Initial contribution
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.WARN)
public class ChannelStateTests {
@Mock
private MqttBrokerConnection connection;
@Mock
private ChannelStateUpdateListener channelStateUpdateListener;
@Mock
private ChannelUID channelUID;
@Spy
private TextValue textValue;
private @Mock MqttBrokerConnection connection;
private @Mock ChannelStateUpdateListener channelStateUpdateListener;
private @Mock ChannelUID channelUID;
private @Spy TextValue textValue;
private ScheduledExecutorService scheduler;
private ChannelConfig config = ChannelConfigBuilder.create("state", "command").build();
@Before
@BeforeEach
public void setUp() {
initMocks(this);
CompletableFuture<Void> voidFutureComplete = new CompletableFuture<>();
voidFutureComplete.complete(null);
doReturn(voidFutureComplete).when(connection).unsubscribeAll();
@@ -87,7 +86,7 @@ public class ChannelStateTests {
scheduler = new ScheduledThreadPoolExecutor(1);
}
@After
@AfterEach
public void tearDown() {
scheduler.shutdownNow();
}
@@ -308,8 +307,8 @@ public class ChannelStateTests {
subject.processMessage("state", datetime.getBytes());
String channelState = value.getChannelState().toString();
assertTrue("Expected '" + channelState + "' to start with '" + datetime + "'",
channelState.startsWith(datetime));
assertTrue(channelState.startsWith(datetime),
"Expected '" + channelState + "' to start with '" + datetime + "'");
assertThat(value.getMQTTpublishValue(null), is(datetime));
}

View File

@@ -13,19 +13,22 @@
package org.openhab.binding.mqtt.generic;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.openhab.binding.mqtt.generic.internal.handler.ThingChannelConstants.*;
import java.util.concurrent.CompletableFuture;
import javax.naming.ConfigurationException;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.openhab.binding.mqtt.generic.internal.handler.GenericMQTTThingHandler;
import org.openhab.binding.mqtt.handler.AbstractBrokerHandler;
import org.openhab.core.config.core.Configuration;
@@ -44,32 +47,21 @@ import org.openhab.core.transform.TransformationService;
*
* @author David Graeff - Initial contribution
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.WARN)
public class ChannelStateTransformationTests {
@Mock
private TransformationService jsonPathService;
@Mock
private TransformationServiceProvider transformationServiceProvider;
@Mock
private ThingHandlerCallback callback;
@Mock
private Thing thing;
@Mock
private AbstractBrokerHandler bridgeHandler;
@Mock
private MqttBrokerConnection connection;
private @Mock TransformationService jsonPathService;
private @Mock TransformationServiceProvider transformationServiceProvider;
private @Mock ThingHandlerCallback callback;
private @Mock Thing thing;
private @Mock AbstractBrokerHandler bridgeHandler;
private @Mock MqttBrokerConnection connection;
private GenericMQTTThingHandler thingHandler;
@Before
@BeforeEach
public void setUp() throws ConfigurationException, MqttException {
initMocks(this);
ThingStatusInfo thingStatus = new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
// Mock the thing: We need the thingUID and the bridgeUID

View File

@@ -13,17 +13,21 @@
package org.openhab.binding.mqtt.generic.internal.handler;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static org.openhab.binding.mqtt.generic.internal.handler.ThingChannelConstants.*;
import java.util.concurrent.CompletableFuture;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.openhab.binding.mqtt.generic.ChannelConfig;
import org.openhab.binding.mqtt.generic.ChannelConfigBuilder;
import org.openhab.binding.mqtt.generic.ChannelState;
@@ -51,26 +55,21 @@ import org.openhab.core.types.RefreshType;
*
* @author David Graeff - Initial contribution
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.WARN)
public class GenericThingHandlerTests {
@Mock
private ThingHandlerCallback callback;
@Mock
private Thing thing;
@Mock
private AbstractBrokerHandler bridgeHandler;
@Mock
private MqttBrokerConnection connection;
private @Mock ThingHandlerCallback callback;
private @Mock Thing thing;
private @Mock AbstractBrokerHandler bridgeHandler;
private @Mock MqttBrokerConnection connection;
private GenericMQTTThingHandler thingHandler;
@Before
@BeforeEach
public void setUp() {
ThingStatusInfo thingStatus = new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
MockitoAnnotations.initMocks(this);
// Mock the thing: We need the thingUID and the bridgeUID
when(thing.getUID()).thenReturn(testGenericThing);
when(thing.getChannels()).thenReturn(thingChannelList);
@@ -100,11 +99,12 @@ public class GenericThingHandlerTests {
doReturn(thingStatus).when(thingHandler).getBridgeStatus();
}
@Test(expected = IllegalArgumentException.class)
@Test
public void initializeWithUnknownThingUID() {
ChannelConfig config = textConfiguration().as(ChannelConfig.class);
thingHandler.createChannelState(config, new ChannelUID(testGenericThing, "test"),
ValueFactory.createValueState(config, unknownChannel.getId()));
assertThrows(IllegalArgumentException.class,
() -> thingHandler.createChannelState(config, new ChannelUID(testGenericThing, "test"),
ValueFactory.createValueState(config, unknownChannel.getId())));
}
@Test

View File

@@ -14,7 +14,8 @@ package org.openhab.binding.mqtt.generic.mapping;
import static java.lang.annotation.ElementType.FIELD;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
@@ -29,12 +30,15 @@ import java.util.stream.Stream;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.openhab.binding.mqtt.generic.mapping.AbstractMqttAttributeClass.AttributeChanged;
import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
@@ -53,6 +57,8 @@ import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
*
* @author David Graeff - Initial contribution
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.WARN)
public class MqttTopicClassMapperTests {
@Retention(RetentionPolicy.RUNTIME)
@Target({ FIELD })
@@ -113,9 +119,8 @@ public class MqttTopicClassMapperTests {
// A completed future is returned for a subscribe call to the attributes
final CompletableFuture<Boolean> future = CompletableFuture.completedFuture(true);
@Before
@BeforeEach
public void setUp() {
MockitoAnnotations.initMocks(this);
doReturn(CompletableFuture.completedFuture(true)).when(connection).subscribe(any(), any());
doReturn(CompletableFuture.completedFuture(true)).when(connection).unsubscribe(any(), any());
injectedFields = (int) Stream.of(countInjectedFields.getClass().getDeclaredFields())
@@ -189,7 +194,7 @@ public class MqttTopicClassMapperTests {
// Check each value if the assignment worked
if (!f.field.getType().isArray()) {
assertNotNull(f.field.getName() + " is null", f.field.get(attributes));
assertNotNull(f.field.get(attributes), f.field.getName() + " is null");
// Consider if a mapToField was used that would manipulate the received value
MQTTvalueTransform mapToField = f.field.getAnnotation(MQTTvalueTransform.class);
String prefix = mapToField != null ? mapToField.prefix() : "";

View File

@@ -14,7 +14,8 @@ package org.openhab.binding.mqtt.generic.mapping;
import static java.lang.annotation.ElementType.FIELD;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
@@ -32,10 +33,13 @@ import java.util.concurrent.TimeoutException;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.openhab.binding.mqtt.generic.mapping.SubscribeFieldToMQTTtopic.FieldChanged;
import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
@@ -44,6 +48,8 @@ import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
*
* @author David Graeff - Initial contribution
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.WARN)
public class SubscribeFieldToMQTTtopicTests {
@Retention(RetentionPolicy.RUNTIME)
@Target({ FIELD })
@@ -101,13 +107,12 @@ public class SubscribeFieldToMQTTtopicTests {
@Mock
FieldChanged fieldChanged;
@Before
@BeforeEach
public void setUp() {
MockitoAnnotations.initMocks(this);
doReturn(CompletableFuture.completedFuture(true)).when(connection).subscribe(any(), any());
}
@Test(expected = TimeoutException.class)
@Test
public void TimeoutIfNoMessageReceive()
throws InterruptedException, NoSuchFieldException, ExecutionException, TimeoutException {
final Field field = Attributes.class.getField("Int");
@@ -115,10 +120,11 @@ public class SubscribeFieldToMQTTtopicTests {
SubscribeFieldToMQTTtopic subscriber = new SubscribeFieldToMQTTtopic(scheduler, field, fieldChanged,
"homie/device123", false);
subscriber.subscribeAndReceive(connection, 1000).get(50, TimeUnit.MILLISECONDS);
assertThrows(TimeoutException.class,
() -> subscriber.subscribeAndReceive(connection, 1000).get(50, TimeUnit.MILLISECONDS));
}
@Test(expected = ExecutionException.class)
@Test
public void MandatoryMissing()
throws InterruptedException, NoSuchFieldException, ExecutionException, TimeoutException {
final Field field = Attributes.class.getField("Int");
@@ -126,7 +132,7 @@ public class SubscribeFieldToMQTTtopicTests {
SubscribeFieldToMQTTtopic subscriber = new SubscribeFieldToMQTTtopic(scheduler, field, fieldChanged,
"homie/device123", true);
subscriber.subscribeAndReceive(connection, 50).get();
assertThrows(ExecutionException.class, () -> subscriber.subscribeAndReceive(connection, 50).get());
}
@Test

View File

@@ -13,11 +13,12 @@
package org.openhab.binding.mqtt.generic.values;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import java.math.BigDecimal;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.openhab.binding.mqtt.generic.mapping.ColorMode;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.HSBType;
@@ -45,10 +46,10 @@ public class ValueTests {
return TypeParser.parseCommand(v.getSupportedCommandTypes(), str);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void illegalTextStateUpdate() {
TextValue v = new TextValue("one,two".split(","));
v.update(p(v, "three"));
assertThrows(IllegalArgumentException.class, () -> v.update(p(v, "three")));
}
public void textStateUpdate() {
@@ -71,34 +72,34 @@ public class ValueTests {
assertThat(((HSBType) v.getChannelState()).getBrightness().intValue(), is(1));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void illegalColorUpdate() {
ColorValue v = new ColorValue(ColorMode.RGB, null, null, 10);
v.update(p(v, "255,255,abc"));
assertThrows(IllegalArgumentException.class, () -> v.update(p(v, "255,255,abc")));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void illegalNumberCommand() {
NumberValue v = new NumberValue(null, null, null, null);
v.update(OnOffType.OFF);
assertThrows(IllegalArgumentException.class, () -> v.update(OnOffType.OFF));
}
@Test(expected = IllegalStateException.class)
@Test
public void illegalPercentCommand() {
PercentageValue v = new PercentageValue(null, null, null, null, null);
v.update(new StringType("demo"));
assertThrows(IllegalStateException.class, () -> v.update(new StringType("demo")));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void illegalOnOffCommand() {
OnOffValue v = new OnOffValue(null, null);
v.update(new DecimalType(101.0));
assertThrows(IllegalArgumentException.class, () -> v.update(new DecimalType(101.0)));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void illegalPercentUpdate() {
PercentageValue v = new PercentageValue(null, null, null, null, null);
v.update(new DecimalType(101.0));
assertThrows(IllegalArgumentException.class, () -> v.update(new DecimalType(101.0)));
}
@Test
@@ -309,10 +310,10 @@ public class ValueTests {
assertEquals(((PercentType) v.getChannelState()).floatValue(), 100.0f, 0.01f);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void percentCalcInvalid() {
PercentageValue v = new PercentageValue(new BigDecimal(10.0), new BigDecimal(110.0), new BigDecimal(1.0), null,
null);
v.update(new DecimalType(9.0));
assertThrows(IllegalArgumentException.class, () -> v.update(new DecimalType(9.0)));
}
}