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

@@ -7,7 +7,7 @@
<parent>
<groupId>org.openhab.addons.itests</groupId>
<artifactId>org.openhab.addons.reactor.itests</artifactId>
<version>2.5.9-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>
</parent>
<artifactId>org.openhab.binding.mqtt.homie.tests</artifactId>

View File

@@ -12,7 +12,7 @@
*/
package org.openhab.binding.mqtt;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
@@ -65,7 +65,7 @@ public class EmbeddedBrokerTools {
}
};
mqttService.addBrokersListener(observer);
assertTrue("Wait for embedded connection client failed", semaphore.tryAcquire(700, TimeUnit.MILLISECONDS));
assertTrue(semaphore.tryAcquire(700, TimeUnit.MILLISECONDS), "Wait for embedded connection client failed");
}
MqttBrokerConnection embeddedConnection = this.embeddedConnection;
if (embeddedConnection == null) {
@@ -84,8 +84,8 @@ public class EmbeddedBrokerTools {
if (embeddedConnection.connectionState() == MqttConnectionState.CONNECTED) {
semaphore.release();
}
assertTrue("Connection " + embeddedConnection.getClientId() + " failed. State: "
+ embeddedConnection.connectionState(), semaphore.tryAcquire(500, TimeUnit.MILLISECONDS));
assertTrue(semaphore.tryAcquire(500, TimeUnit.MILLISECONDS), "Connection " + embeddedConnection.getClientId()
+ " failed. State: " + embeddedConnection.connectionState());
return embeddedConnection;
}
}

View File

@@ -13,10 +13,11 @@
package org.openhab.binding.mqtt;
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.*;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.mockito.MockitoAnnotations.openMocks;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
@@ -31,17 +32,9 @@ import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.types.UnDefType;
import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
import org.openhab.core.io.transport.mqtt.MqttConnectionObserver;
import org.openhab.core.io.transport.mqtt.MqttConnectionState;
import org.openhab.core.io.transport.mqtt.MqttService;
import org.openhab.core.test.java.JavaOSGiTest;
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.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.openhab.binding.mqtt.generic.ChannelState;
@@ -58,6 +51,14 @@ import org.openhab.binding.mqtt.homie.internal.homie300.Property;
import org.openhab.binding.mqtt.homie.internal.homie300.PropertyAttributes;
import org.openhab.binding.mqtt.homie.internal.homie300.PropertyAttributes.DataTypeEnum;
import org.openhab.binding.mqtt.homie.internal.homie300.PropertyHelper;
import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
import org.openhab.core.io.transport.mqtt.MqttConnectionObserver;
import org.openhab.core.io.transport.mqtt.MqttConnectionState;
import org.openhab.core.io.transport.mqtt.MqttService;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.test.java.JavaOSGiTest;
import org.openhab.core.types.UnDefType;
/**
* A full implementation test, that starts the embedded MQTT broker and publishes a homie device tree.
@@ -74,13 +75,13 @@ public class HomieImplementationTest extends JavaOSGiTest {
private MqttBrokerConnection connection;
private int registeredTopics = 100;
private AutoCloseable mocksCloseable;
// The handler is not tested here, so just mock the callback
@Mock
DeviceCallback callback;
private @Mock DeviceCallback callback;
// A handler mock is required to verify that channel value changes have been received
@Mock
HomieThingHandler handler;
private @Mock HomieThingHandler handler;
private ScheduledExecutorService scheduler;
@@ -93,10 +94,10 @@ public class HomieImplementationTest extends JavaOSGiTest {
private String propertyTestTopic;
@Before
public void setUp() throws InterruptedException, ExecutionException, TimeoutException {
@BeforeEach
public void beforeEach() throws Exception {
registerVolatileStorageService();
initMocks(this);
mocksCloseable = openMocks(this);
mqttService = getService(MqttService.class);
embeddedConnection = new EmbeddedBrokerTools().waitForConnection(mqttService);
@@ -152,13 +153,14 @@ public class HomieImplementationTest extends JavaOSGiTest {
scheduler = new ScheduledThreadPoolExecutor(6);
}
@After
public void tearDown() throws InterruptedException, ExecutionException, TimeoutException {
@AfterEach
public void afterEach() throws Exception {
if (connection != null) {
connection.removeConnectionObserver(failIfChange);
connection.stop().get(500, TimeUnit.MILLISECONDS);
}
scheduler.shutdownNow();
mocksCloseable.close();
}
@Test
@@ -167,8 +169,8 @@ public class HomieImplementationTest extends JavaOSGiTest {
CountDownLatch c = new CountDownLatch(registeredTopics - 4);
connection.subscribe(DEVICE_TOPIC + "/testnode/#", (topic, payload) -> c.countDown()).get(5000,
TimeUnit.MILLISECONDS);
assertTrue("Connection " + connection.getClientId() + " not retrieving all topics ",
c.await(5000, TimeUnit.MILLISECONDS));
assertTrue(c.await(5000, TimeUnit.MILLISECONDS),
"Connection " + connection.getClientId() + " not retrieving all topics ");
}
@Test
@@ -311,11 +313,8 @@ public class HomieImplementationTest extends JavaOSGiTest {
property.getChannelState().publishValue(OnOffType.ON).get();
// No value is expected to be retained on this MQTT topic
waitForAssert(() -> {
try {
WaitForTopicValue w = new WaitForTopicValue(embeddedConnection, propertyTestTopic + "/set");
assertNull(w.waitForTopicValue(50));
} catch (InterruptedException | ExecutionException e) {
}
WaitForTopicValue w = new WaitForTopicValue(embeddedConnection, propertyTestTopic + "/set");
assertNull(w.waitForTopicValue(50));
}, 500, 100);
}
}