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,7 +13,8 @@
package org.openhab.binding.network.internal;
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.*;
import static org.mockito.Mockito.*;
@@ -25,12 +26,15 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
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.ArgumentCaptor;
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.network.internal.toberemoved.cache.ExpiringCacheAsync;
import org.openhab.binding.network.internal.toberemoved.cache.ExpiringCacheHelper;
import org.openhab.binding.network.internal.utils.NetworkUtils;
@@ -43,26 +47,20 @@ import org.openhab.binding.network.internal.utils.PingResult;
*
* @author David Graeff - Initial contribution
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.WARN)
public class PresenceDetectionTest {
static long CACHETIME = 2000L;
@Mock
NetworkUtils networkUtils;
private static final long CACHETIME = 2000L;
@Mock
PresenceDetectionListener listener;
private PresenceDetection subject;
@Mock
ExecutorService executorService;
private @Mock Consumer<PresenceDetectionValue> callback;
private @Mock ExecutorService executorService;
private @Mock PresenceDetectionListener listener;
private @Mock NetworkUtils networkUtils;
@Mock
Consumer<PresenceDetectionValue> callback;
PresenceDetection subject;
@Before
@BeforeEach
public void setUp() throws UnknownHostException {
MockitoAnnotations.initMocks(this);
// Mock an interface
when(networkUtils.getInterfaceNames()).thenReturn(Collections.singleton("TESTinterface"));
doReturn(ArpPingUtilEnum.IPUTILS_ARPING).when(networkUtils).determineNativeARPpingMethod(anyString());
@@ -86,7 +84,7 @@ public class PresenceDetectionTest {
assertThat(subject.pingMethod, is(IpPingMethodEnum.WINDOWS_PING));
}
@After
@AfterEach
public void shutDown() {
subject.waitForPresenceDetection();
}

View File

@@ -13,9 +13,10 @@
package org.openhab.binding.network.internal;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.Test;
import org.junit.jupiter.api.Test;
/**
* Tests cases for {@see PresenceDetectionValue}

View File

@@ -13,12 +13,15 @@
package org.openhab.binding.network.internal;
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.openhab.binding.network.internal.WakeOnLanPacketSender.*;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.openhab.core.util.HexUtils;
/**
@@ -26,6 +29,7 @@ import org.openhab.core.util.HexUtils;
*
* @author Wouter Born - Initial contribution
*/
@Timeout(value = 10, unit = TimeUnit.SECONDS)
public class WakeOnLanPacketSenderTest {
private void assertValidMagicPacket(byte[] macBytes, byte[] packet) {
@@ -75,23 +79,23 @@ public class WakeOnLanPacketSenderTest {
assertValidMagicPacket(HexUtils.hexToBytes("6f70656e4841"), actualPacket);
}
@Test(expected = IllegalStateException.class, timeout = 10000)
@Test
public void sendWithEmptyMacAddressThrowsException() {
new WakeOnLanPacketSender("").sendPacket();
assertThrows(IllegalStateException.class, () -> new WakeOnLanPacketSender("").sendPacket());
}
@Test(expected = IllegalStateException.class, timeout = 10000)
@Test
public void sendWithTooShortMacAddressThrowsException() {
new WakeOnLanPacketSender("6f:70:65:6e:48").sendPacket();
assertThrows(IllegalStateException.class, () -> new WakeOnLanPacketSender("6f:70:65:6e:48").sendPacket());
}
@Test(expected = IllegalStateException.class, timeout = 10000)
@Test
public void sendWithTooLongMacAddressThrowsException() {
new WakeOnLanPacketSender("6f:70:65:6e:48:41:42").sendPacket();
assertThrows(IllegalStateException.class, () -> new WakeOnLanPacketSender("6f:70:65:6e:48:41:42").sendPacket());
}
@Test(expected = IllegalStateException.class, timeout = 10000)
@Test
public void sendWithUnsupportedSeparatorInMacAddressThrowsException() {
new WakeOnLanPacketSender("6f=70=65=6e=48=41").sendPacket();
assertThrows(IllegalStateException.class, () -> new WakeOnLanPacketSender("6f=70=65=6e=48=41").sendPacket());
}
}

View File

@@ -13,16 +13,16 @@
package org.openhab.binding.network.internal.dhcp;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeTrue;
import static org.mockito.Matchers.eq;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
import java.io.IOException;
import java.net.InetAddress;
import java.net.SocketException;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.openhab.binding.network.internal.dhcp.DHCPPacket.BadPacketException;
/**

View File

@@ -13,17 +13,20 @@
package org.openhab.binding.network.internal.discovery;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.initMocks;
import java.util.Collections;
import org.junit.Assert;
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.ArgumentCaptor;
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.network.internal.NetworkBindingConstants;
import org.openhab.binding.network.internal.PresenceDetectionValue;
import org.openhab.core.config.discovery.DiscoveryListener;
@@ -34,18 +37,16 @@ import org.openhab.core.config.discovery.DiscoveryResult;
*
* @author David Graeff - Initial contribution
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.WARN)
public class DiscoveryTest {
private final String ip = "127.0.0.1";
@Mock
PresenceDetectionValue value;
private @Mock PresenceDetectionValue value;
private @Mock DiscoveryListener listener;
@Mock
DiscoveryListener listener;
@Before
@BeforeEach
public void setUp() {
initMocks(this);
when(value.getHostAddress()).thenReturn(ip);
when(value.getLowestLatency()).thenReturn(10.0);
when(value.isReachable()).thenReturn(true);
@@ -65,8 +66,8 @@ public class DiscoveryTest {
d.partialDetectionResult(value);
verify(listener).thingDiscovered(any(), result.capture());
DiscoveryResult dresult = result.getValue();
Assert.assertThat(dresult.getThingUID(), is(NetworkDiscoveryService.createPingUID(ip)));
Assert.assertThat(dresult.getProperties().get(NetworkBindingConstants.PARAMETER_HOSTNAME), is(ip));
assertThat(dresult.getThingUID(), is(NetworkDiscoveryService.createPingUID(ip)));
assertThat(dresult.getProperties().get(NetworkBindingConstants.PARAMETER_HOSTNAME), is(ip));
}
@Test
@@ -83,8 +84,8 @@ public class DiscoveryTest {
d.partialDetectionResult(value);
verify(listener).thingDiscovered(any(), result.capture());
DiscoveryResult dresult = result.getValue();
Assert.assertThat(dresult.getThingUID(), is(NetworkDiscoveryService.createServiceUID(ip, 1010)));
Assert.assertThat(dresult.getProperties().get(NetworkBindingConstants.PARAMETER_HOSTNAME), is(ip));
Assert.assertThat(dresult.getProperties().get(NetworkBindingConstants.PARAMETER_PORT), is(1010));
assertThat(dresult.getThingUID(), is(NetworkDiscoveryService.createServiceUID(ip, 1010)));
assertThat(dresult.getProperties().get(NetworkBindingConstants.PARAMETER_HOSTNAME), is(ip));
assertThat(dresult.getProperties().get(NetworkBindingConstants.PARAMETER_PORT), is(1010));
}
}

View File

@@ -13,17 +13,20 @@
package org.openhab.binding.network.internal.handler;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.initMocks;
import org.junit.Assert;
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.ArgumentCaptor;
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.network.internal.NetworkBindingConfiguration;
import org.openhab.binding.network.internal.NetworkBindingConstants;
import org.openhab.binding.network.internal.PresenceDetection;
@@ -45,17 +48,16 @@ import org.openhab.core.thing.binding.ThingHandlerCallback;
*
* @author David Graeff - Initial contribution
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.WARN)
public class NetworkHandlerTest extends JavaTest {
private ThingUID thingUID = new ThingUID("network", "ttype", "ping");
@Mock
private ThingHandlerCallback callback;
@Mock
private Thing thing;
private @Mock ThingHandlerCallback callback;
private @Mock Thing thing;
@Before
@BeforeEach
public void setUp() {
initMocks(this);
when(thing.getUID()).thenReturn(thingUID);
}
@@ -91,7 +93,7 @@ public class NetworkHandlerTest extends JavaTest {
public void tcpDeviceInitTests() {
NetworkBindingConfiguration config = new NetworkBindingConfiguration();
NetworkHandler handler = spy(new NetworkHandler(thing, true, config));
Assert.assertThat(handler.isTCPServiceDevice(), is(true));
assertThat(handler.isTCPServiceDevice(), is(true));
handler.setCallback(callback);
// Port is missing, should make the device OFFLINE
when(thing.getConfiguration()).thenAnswer(a -> {
@@ -103,9 +105,8 @@ public class NetworkHandlerTest extends JavaTest {
// Check that we are offline
ArgumentCaptor<ThingStatusInfo> statusInfoCaptor = ArgumentCaptor.forClass(ThingStatusInfo.class);
verify(callback).statusUpdated(eq(thing), statusInfoCaptor.capture());
Assert.assertThat(statusInfoCaptor.getValue().getStatus(), is(equalTo(ThingStatus.OFFLINE)));
Assert.assertThat(statusInfoCaptor.getValue().getStatusDetail(),
is(equalTo(ThingStatusDetail.CONFIGURATION_ERROR)));
assertThat(statusInfoCaptor.getValue().getStatus(), is(equalTo(ThingStatus.OFFLINE)));
assertThat(statusInfoCaptor.getValue().getStatusDetail(), is(equalTo(ThingStatusDetail.CONFIGURATION_ERROR)));
}
@Test

View File

@@ -12,12 +12,12 @@
*/
package org.openhab.binding.network.internal.toberemoved.cache;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.function.Consumer;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.openhab.binding.network.internal.toberemoved.cache.ExpiringCacheAsync.ExpiringCacheUpdate;
@@ -27,18 +27,17 @@ import org.openhab.binding.network.internal.toberemoved.cache.ExpiringCacheAsync
* @author David Graeff - Initial contribution
*/
public class ExpiringCacheAsyncTest {
@SuppressWarnings("unused")
@Test(expected = IllegalArgumentException.class)
@Test
public void testConstructorWrongCacheTime() {
assertThrows(IllegalArgumentException.class, () ->
// Fail if cache time is <= 0
new ExpiringCacheAsync<>(0, () -> {
});
}));
}
@SuppressWarnings("unused")
@Test(expected = IllegalArgumentException.class)
@Test
public void testConstructorNoRefrehCommand() {
new ExpiringCacheAsync<>(2000, null);
assertThrows(IllegalArgumentException.class, () -> new ExpiringCacheAsync<>(2000, null));
}
@Test

View File

@@ -12,10 +12,11 @@
*/
package org.openhab.binding.network.internal.utils;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Optional;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;
/**
* Tests the parser which extracts latency values from the output of the ping command.
@@ -34,8 +35,8 @@ public class LatencyParserTest {
Optional<Double> resultLatency = latencyParser.parseLatency(input);
// Assert
Assert.assertTrue(resultLatency.isPresent());
Assert.assertEquals(1.225, resultLatency.get(), 0);
assertTrue(resultLatency.isPresent());
assertEquals(1.225, resultLatency.get(), 0);
}
@Test
@@ -54,7 +55,7 @@ public class LatencyParserTest {
Optional<Double> resultLatency = latencyParser.parseLatency(inputLine);
// Assert
Assert.assertFalse(resultLatency.isPresent());
assertFalse(resultLatency.isPresent());
}
}
}