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

@@ -12,19 +12,19 @@
*/
package org.openhab.binding.nibeheatpump.internal.handler;
import static org.junit.Assert.assertEquals;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.openhab.binding.nibeheatpump.internal.models.PumpModel;
import org.openhab.binding.nibeheatpump.internal.models.VariableInformation;
import org.openhab.core.io.transport.serial.SerialPortManager;
@@ -38,7 +38,7 @@ import org.openhab.core.types.Command;
*
* @author Jevgeni Kiski - Initial contribution
*/
@RunWith(Parameterized.class)
@ExtendWith(MockitoExtension.class)
public class NibeHeatPumpHandlerCommand2NibeTest {
private NibeHeatPumpHandler product; // the class under test
private Method m;
@@ -46,13 +46,8 @@ public class NibeHeatPumpHandlerCommand2NibeTest {
private Class<?>[] parameterTypes;
private Object[] parameters;
private int fCoilAddress;
private Command fCommand;
private int fExpected;
private @Mock SerialPortManager serialPortManager;
@Parameterized.Parameters(name = "{index}: f({0}, {1})={2}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] { { 47028, new DecimalType("-1"), (byte) 0xFF },
{ 48132, new DecimalType("0"), 0 }, { 48132, new StringType("0"), 0 },
@@ -63,16 +58,8 @@ public class NibeHeatPumpHandlerCommand2NibeTest {
{ 47371, OnOffType.from(true), 0x1 }, { 47371, OnOffType.from(false), 0x0 }, });
}
public NibeHeatPumpHandlerCommand2NibeTest(final int coilAddress, final Command command, final int expected) {
this.fCoilAddress = coilAddress;
this.fCommand = command;
this.fExpected = expected;
}
@Before
@BeforeEach
public void setUp() throws Exception {
initMocks(this);
product = new NibeHeatPumpHandler(null, PumpModel.F1X55, serialPortManager);
parameterTypes = new Class[2];
parameterTypes[0] = VariableInformation.class;
@@ -82,13 +69,15 @@ public class NibeHeatPumpHandlerCommand2NibeTest {
parameters = new Object[2];
}
@Test
public void convertNibeValueToStateTest() throws InvocationTargetException, IllegalAccessException {
VariableInformation varInfo = VariableInformation.getVariableInfo(PumpModel.F1X55, fCoilAddress);
@ParameterizedTest
@MethodSource("data")
public void convertNibeValueToStateTest(final int coilAddress, final Command command, final int expected)
throws InvocationTargetException, IllegalAccessException {
VariableInformation varInfo = VariableInformation.getVariableInfo(PumpModel.F1X55, coilAddress);
parameters[0] = varInfo;
parameters[1] = fCommand;
parameters[1] = command;
int value = (int) m.invoke(product, parameters);
assertEquals(fExpected, value);
assertEquals(expected, value);
}
}

View File

@@ -12,8 +12,7 @@
*/
package org.openhab.binding.nibeheatpump.internal.handler;
import static org.junit.Assert.assertEquals;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@@ -21,11 +20,12 @@ import java.text.DecimalFormatSymbols;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.openhab.binding.nibeheatpump.internal.models.PumpModel;
import org.openhab.binding.nibeheatpump.internal.models.VariableInformation;
import org.openhab.core.io.transport.serial.SerialPortManager;
@@ -36,7 +36,7 @@ import org.openhab.core.types.State;
*
* @author Jevgeni Kiski - Initial contribution
*/
@RunWith(Parameterized.class)
@ExtendWith(MockitoExtension.class)
public class NibeHeatPumpHandlerNibe2StateTest {
// we need to get the decimal separator of the default locale for our tests
@@ -47,15 +47,9 @@ public class NibeHeatPumpHandlerNibe2StateTest {
private Method m;
private Class<?>[] parameterTypes;
private Object[] parameters;
private int fCoilAddress;
private int fValue;
private String fFormat;
private String fType;
private String fExpected;
private @Mock SerialPortManager serialPortManager;
@Parameterized.Parameters(name = "{index}: f({0}, {1}, {3})={4}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] { //
{ 47028, 0xFF, "%d", "Number", "-1" }, //
@@ -87,19 +81,8 @@ public class NibeHeatPumpHandlerNibe2StateTest {
});
}
public NibeHeatPumpHandlerNibe2StateTest(final int coilAddress, final int value, final String format,
final String type, final String expected) {
this.fCoilAddress = coilAddress;
this.fValue = value;
this.fFormat = format;
this.fType = type;
this.fExpected = expected;
}
@Before
@BeforeEach
public void setUp() throws Exception {
initMocks(this);
product = new NibeHeatPumpHandler(null, PumpModel.F1X55, serialPortManager);
parameterTypes = new Class[3];
parameterTypes[0] = VariableInformation.class;
@@ -110,14 +93,16 @@ public class NibeHeatPumpHandlerNibe2StateTest {
parameters = new Object[3];
}
@Test
public void convertNibeValueToStateTest() throws InvocationTargetException, IllegalAccessException {
VariableInformation varInfo = VariableInformation.getVariableInfo(PumpModel.F1X55, fCoilAddress);
@ParameterizedTest
@MethodSource("data")
public void convertNibeValueToStateTest(final int coilAddress, final int value, final String format,
final String type, final String expected) throws InvocationTargetException, IllegalAccessException {
VariableInformation varInfo = VariableInformation.getVariableInfo(PumpModel.F1X55, coilAddress);
parameters[0] = varInfo;
parameters[1] = fValue;
parameters[2] = fType;
parameters[1] = value;
parameters[2] = type;
State state = (State) m.invoke(product, parameters);
assertEquals(fExpected, state.format(fFormat));
assertEquals(expected, state.format(format));
}
}

View File

@@ -12,11 +12,11 @@
*/
package org.openhab.binding.nibeheatpump.internal.message;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.openhab.binding.nibeheatpump.internal.NibeHeatPumpException;
import org.openhab.core.util.HexUtils;
@@ -182,20 +182,20 @@ public class ModbusDataReadOutMessageTest {
checkRegisters(message, expectedValues);
}
@Test(expected = NibeHeatPumpException.class)
public void badCrcTest() throws NibeHeatPumpException {
@Test
public void badCrcTest() {
final String message = "5C0020685001A81F0100A86400FDA7D003449C1E004F9CA000509C7800519C0301529C1B01879C14014E9CC601479C010115B9B0FF3AB94B00C9AF0000489C0D014C9CE7004B9C0000FFFF0000FFFF0000FFFF000044";
final byte[] msg = HexUtils.hexToBytes(message);
MessageFactory.getMessage(msg);
assertThrows(NibeHeatPumpException.class, () -> MessageFactory.getMessage(msg));
}
@Test(expected = NibeHeatPumpException.class)
public void notModbusDataReadOutMessageTest() throws NibeHeatPumpException {
@Test
public void notModbusDataReadOutMessageTest() {
final String message = "519C0301529C1B01879C14014E9CC601479C010115B9B0FF3AB94B00C9AF0000489C0D014C9CE7004B9C0000FFFF0000FFFF0000FFFF000044";
final byte[] msg = HexUtils.hexToBytes(message);
new ModbusDataReadOutMessage(msg);
assertThrows(NibeHeatPumpException.class, () -> new ModbusDataReadOutMessage(msg));
}
private void checkRegisters(final String message, final ArrayList<ModbusValue> expectedRegs)

View File

@@ -12,9 +12,9 @@
*/
package org.openhab.binding.nibeheatpump.internal.message;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.openhab.binding.nibeheatpump.internal.NibeHeatPumpException;
import org.openhab.core.util.HexUtils;
@@ -42,17 +42,17 @@ public class ModbusReadRequestMessageTest {
assertEquals(coilAddress, m.getCoilAddress());
}
@Test(expected = NibeHeatPumpException.class)
public void badCrcTest() throws NibeHeatPumpException {
@Test
public void badCrcTest() {
final String strMessage = "C069023930A1";
final byte[] msg = HexUtils.hexToBytes(strMessage);
new ModbusReadRequestMessage(msg);
assertThrows(NibeHeatPumpException.class, () -> new ModbusReadRequestMessage(msg));
}
@Test(expected = NibeHeatPumpException.class)
public void notReadRequestMessageTest() throws NibeHeatPumpException {
@Test
public void notReadRequestMessageTest() {
final String strMessage = "C169023930A2";
final byte[] byteMessage = HexUtils.hexToBytes(strMessage);
new ModbusReadRequestMessage(byteMessage);
assertThrows(NibeHeatPumpException.class, () -> new ModbusReadRequestMessage(byteMessage));
}
}

View File

@@ -12,9 +12,9 @@
*/
package org.openhab.binding.nibeheatpump.internal.message;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.openhab.binding.nibeheatpump.internal.NibeHeatPumpException;
import org.openhab.core.util.HexUtils;
@@ -45,17 +45,17 @@ public class ModbusReadResponseMessageTest {
assertEquals(value, m.getValue());
}
@Test(expected = NibeHeatPumpException.class)
public void badCrcTest() throws NibeHeatPumpException {
@Test
public void badCrcTest() {
final String strMessage = "5C00206A060102030405064C";
final byte[] byteMessage = HexUtils.hexToBytes(strMessage);
MessageFactory.getMessage(byteMessage);
assertThrows(NibeHeatPumpException.class, () -> MessageFactory.getMessage(byteMessage));
}
@Test(expected = NibeHeatPumpException.class)
public void notReadResponseMessageTest() throws NibeHeatPumpException {
@Test
public void notReadResponseMessageTest() {
final String strMessage = "5C00206B060102030405064A";
final byte[] byteMessage = HexUtils.hexToBytes(strMessage);
new ModbusReadResponseMessage(byteMessage);
assertThrows(NibeHeatPumpException.class, () -> new ModbusReadResponseMessage(byteMessage));
}
}

View File

@@ -12,10 +12,10 @@
*/
package org.openhab.binding.nibeheatpump.internal.message;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.binding.nibeheatpump.internal.NibeHeatPumpException;
import org.openhab.core.util.HexUtils;
@@ -26,7 +26,7 @@ import org.openhab.core.util.HexUtils;
*/
public class ModbusWriteRequestMessageTest {
@Before
@BeforeEach
public void Before() {
}
@@ -50,17 +50,17 @@ public class ModbusWriteRequestMessageTest {
assertEquals(value, m.getValue());
}
@Test(expected = NibeHeatPumpException.class)
public void badCrcTest() throws NibeHeatPumpException {
@Test
public void badCrcTest() {
final String strMessage = "C06B06393006120F00BA";
final byte[] msg = HexUtils.hexToBytes(strMessage);
new ModbusWriteRequestMessage(msg);
assertThrows(NibeHeatPumpException.class, () -> new ModbusWriteRequestMessage(msg));
}
@Test(expected = NibeHeatPumpException.class)
public void notWriteRequestMessageTest() throws NibeHeatPumpException {
@Test
public void notWriteRequestMessageTest() {
final String strMessage = "C06A06393006120F00BF";
final byte[] byteMessage = HexUtils.hexToBytes(strMessage);
new ModbusWriteRequestMessage(byteMessage);
assertThrows(NibeHeatPumpException.class, () -> new ModbusWriteRequestMessage(byteMessage));
}
}

View File

@@ -12,10 +12,10 @@
*/
package org.openhab.binding.nibeheatpump.internal.message;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.binding.nibeheatpump.internal.NibeHeatPumpException;
import org.openhab.core.util.HexUtils;
@@ -26,7 +26,7 @@ import org.openhab.core.util.HexUtils;
*/
public class ModbusWriteResponseMessageTest {
@Before
@BeforeEach
public void Before() {
}
@@ -62,17 +62,17 @@ public class ModbusWriteResponseMessageTest {
assertEquals(false, m.isSuccessfull());
}
@Test(expected = NibeHeatPumpException.class)
public void badCrcTest() throws NibeHeatPumpException {
@Test
public void badCrcTest() {
final String strMessage = "5C00206C01004A";
final byte[] msg = HexUtils.hexToBytes(strMessage);
new ModbusWriteResponseMessage(msg);
assertThrows(NibeHeatPumpException.class, () -> new ModbusWriteResponseMessage(msg));
}
@Test(expected = NibeHeatPumpException.class)
public void notWriteResponseMessageTest() throws NibeHeatPumpException {
@Test
public void notWriteResponseMessageTest() {
final String strMessage = "5C00206B060102030405064A";
final byte[] byteMessage = HexUtils.hexToBytes(strMessage);
new ModbusWriteResponseMessage(byteMessage);
assertThrows(NibeHeatPumpException.class, () -> new ModbusWriteResponseMessage(byteMessage));
}
}

View File

@@ -12,15 +12,16 @@
*/
package org.openhab.binding.nibeheatpump.internal.message;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
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.Timeout;
import org.openhab.binding.nibeheatpump.internal.protocol.NibeHeatPumpProtocolContext;
import org.openhab.binding.nibeheatpump.internal.protocol.NibeHeatPumpProtocolDefaultContext;
import org.openhab.core.util.HexUtils;
@@ -63,7 +64,7 @@ public class NibeHeatPumpProtocolTest {
}
};
@Before
@BeforeEach
public void Before() {
ackRequestCount = 0;
nakRequestCount = 0;
@@ -74,7 +75,8 @@ public class NibeHeatPumpProtocolTest {
mockupContext.msg().clear();
}
@Test(timeout = 1000)
@Test
@Timeout(value = 1, unit = TimeUnit.SECONDS)
public void test() {
//@formatter:off
final String strTestData =
@@ -132,24 +134,24 @@ public class NibeHeatPumpProtocolTest {
String expect;
expect = "5C001962189600E1010200000000800000000000020914340001000005B8";
Assert.assertArrayEquals(HexUtils.hexToBytes(expect), receivedMsgs.get(0));
assertArrayEquals(HexUtils.hexToBytes(expect), receivedMsgs.get(0));
expect = "5C00206850449C9600489C88014C9C2D014E9CCF004D9CE0014F9C3200509C0400519C8201529C6B02569C3E00C9AF000001A8F600FDA77E02FAA90F0098A9DC27FFFF0000A0A93A04FFFF00009CA9FD19FFFF000081";
Assert.assertArrayEquals(HexUtils.hexToBytes(expect), receivedMsgs.get(1));
assertArrayEquals(HexUtils.hexToBytes(expect), receivedMsgs.get(1));
expect = "5C001962189600DF01020000000080000000000002091434000100000586";
Assert.assertArrayEquals(HexUtils.hexToBytes(expect), receivedMsgs.get(2));
assertArrayEquals(HexUtils.hexToBytes(expect), receivedMsgs.get(2));
expect = "5C0019600079";
Assert.assertArrayEquals(HexUtils.hexToBytes(expect), receivedMsgs.get(3));
assertArrayEquals(HexUtils.hexToBytes(expect), receivedMsgs.get(3));
expect = "5C00206851449C2500489CFC004C9CF1004E9CC7014D9C0B024F9C2500509C3300519C0B01529C5C5C01569C3100C9AF000001A80C01FDA716FAFAA9070098A91B1BFFFF0000A0A9CA02FFFF00009CA99212FFFF0000BE";
Assert.assertArrayEquals(HexUtils.hexToBytes(expect), receivedMsgs.get(4));
assertArrayEquals(HexUtils.hexToBytes(expect), receivedMsgs.get(4));
expect = "5C00206852449C2500489CFE004C9CF2004E9CD4014D9CFB014F9C2500509C3700519C0D01529C5C5C01569C3200C9AF000001A80C01FDA712FAFAA9070098A95C5C1BFFFF0000A0A9D102FFFF00009CA9B412FFFF00007F";
Assert.assertArrayEquals(HexUtils.hexToBytes(expect), receivedMsgs.get(5));
assertArrayEquals(HexUtils.hexToBytes(expect), receivedMsgs.get(5));
expect = "5C00206850449C2600489CF6004C9CF1004E9CD6014D9C0C024F9C4500509C3F00519CF100529C0401569CD500C9AF000001A80C01FDA799FAFAA9020098A91A1BFFFF0000A0A9CA02FFFF00009CA99212FFFF0000C5";
Assert.assertArrayEquals(HexUtils.hexToBytes(expect), receivedMsgs.get(6));
assertArrayEquals(HexUtils.hexToBytes(expect), receivedMsgs.get(6));
}
}

View File

@@ -12,17 +12,17 @@
*/
package org.openhab.binding.nibeheatpump.internal.models;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/**
* @author Pauli Anttila - Initial contribution
*/
public class PumpModelTest {
@Before
@BeforeEach
public void Before() {
}
@@ -54,8 +54,8 @@ public class PumpModelTest {
assertEquals(PumpModel.F470, pumpModel);
}
@Test(expected = IllegalArgumentException.class)
@Test
public void badPumpModelTest() {
PumpModel.getPumpModel("XXXX");
assertThrows(IllegalArgumentException.class, () -> PumpModel.getPumpModel("XXXX"));
}
}

View File

@@ -12,17 +12,17 @@
*/
package org.openhab.binding.nibeheatpump.internal.models;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/**
* @author Pauli Anttila - Initial contribution
*/
public class VariableInformationTest {
@Before
@BeforeEach
public void Before() {
}