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.io.hueemulation.internal.automation;
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 java.time.LocalDateTime;
import java.util.Collections;
@@ -22,6 +23,11 @@ import java.util.Random;
import java.util.TreeMap;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.core.automation.Condition;
import org.openhab.core.automation.util.ConditionBuilder;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.items.GroupItem;
import org.openhab.core.library.items.ContactItem;
@@ -30,11 +36,6 @@ import org.openhab.core.library.items.SwitchItem;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.OpenClosedType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openhab.core.automation.Condition;
import org.openhab.core.automation.util.ConditionBuilder;
import org.openhab.io.hueemulation.internal.DeviceType;
import org.openhab.io.hueemulation.internal.RuleUtils;
import org.openhab.io.hueemulation.internal.dto.HueDataStore;
@@ -64,7 +65,7 @@ public class RuleConditionHandlerTests {
}
}
@Before
@BeforeEach
public void setUp() {
ds = new HueDataStore();
@@ -76,12 +77,12 @@ public class RuleConditionHandlerTests {
new HueGroupEntry("name", new GroupItem("white", new NumberItem("number")), DeviceType.SwitchType));
}
@After
@AfterEach
public void tearDown() {
RuleUtils.random = new Random();
}
@Test(expected = IllegalStateException.class)
@Test
public void itemNotExisting() {
Configuration configuration = new Configuration();
configuration.put("address", "/groups/9/action");
@@ -89,7 +90,7 @@ public class RuleConditionHandlerTests {
configuration.put("value", "");
Condition c = ConditionBuilder.create().withId("a").withTypeUID(HueRuleConditionHandler.MODULE_TYPE_ID)
.withConfiguration(configuration).build();
new HueRuleConditionHandler(c, ds);
assertThrows(IllegalStateException.class, () -> new HueRuleConditionHandler(c, ds));
}
@Test

View File

@@ -29,19 +29,22 @@ import java.util.logging.Logger;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import org.openhab.core.events.EventPublisher;
import org.openhab.core.items.MetadataRegistry;
import org.openhab.core.net.NetworkAddressService;
import org.openhab.core.storage.Storage;
import org.openhab.core.storage.StorageService;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.logging.LoggingFeature.Verbosity;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.openhab.core.events.EventPublisher;
import org.openhab.core.items.MetadataRegistry;
import org.openhab.core.net.NetworkAddressService;
import org.openhab.core.storage.Storage;
import org.openhab.core.storage.StorageService;
import org.openhab.io.hueemulation.internal.ConfigStore;
import org.openhab.io.hueemulation.internal.rest.mocks.ConfigStoreWithoutMetadata;
import org.openhab.io.hueemulation.internal.rest.mocks.DummyMetadataRegistry;
@@ -58,12 +61,13 @@ import org.osgi.service.cm.ConfigurationAdmin;
*
* @author David Graeff - Initial contribution
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.WARN)
public class CommonSetup {
public UserManagement userManagement;
@Mock
public EventPublisher eventPublisher;
public @Mock EventPublisher eventPublisher;
public ConfigStore cs;
@@ -102,7 +106,6 @@ public class CommonSetup {
public String basePath;
public CommonSetup(boolean withMetadata) throws IOException {
MockitoAnnotations.initMocks(this);
when(configAdmin.getConfiguration(anyString())).thenReturn(configAdminConfig);
when(configAdmin.getConfiguration(anyString(), any())).thenReturn(configAdminConfig);
Dictionary<String, Object> mockProperties = new Hashtable<>();

View File

@@ -13,7 +13,7 @@
package org.openhab.io.hueemulation.internal.rest;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
import java.io.IOException;
@@ -21,15 +21,15 @@ import java.util.Collections;
import java.util.concurrent.ScheduledExecutorService;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.glassfish.jersey.server.ResourceConfig;
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.items.Metadata;
import org.openhab.core.items.MetadataKey;
import org.openhab.core.library.items.SwitchItem;
import org.glassfish.jersey.server.ResourceConfig;
import org.hamcrest.CoreMatchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openhab.io.hueemulation.internal.ConfigStore;
import org.openhab.io.hueemulation.internal.dto.HueLightEntry;
import org.openhab.io.hueemulation.internal.dto.HueStatePlug;
@@ -47,7 +47,7 @@ public class ItemUIDtoHueIDMappingTests {
LightsAndGroups lightsAndGroups = new LightsAndGroups();
@Before
@BeforeEach
public void setUp() throws IOException {
commonSetup = new CommonSetup(true);
commonSetup.start(new ResourceConfig());
@@ -61,7 +61,7 @@ public class ItemUIDtoHueIDMappingTests {
lightsAndGroups.activate();
}
@After
@AfterEach
public void tearDown() {
commonSetup.dispose();
}

View File

@@ -13,7 +13,8 @@
package org.openhab.io.hueemulation.internal.rest;
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.argThat;
import static org.mockito.Mockito.verify;
@@ -23,6 +24,10 @@ import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.core.events.Event;
import org.openhab.core.items.GroupItem;
import org.openhab.core.items.ItemRegistry;
@@ -31,10 +36,6 @@ import org.openhab.core.library.items.ColorItem;
import org.openhab.core.library.items.SwitchItem;
import org.openhab.core.library.types.HSBType;
import org.openhab.core.library.types.OnOffType;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openhab.io.hueemulation.internal.ConfigStore;
import org.openhab.io.hueemulation.internal.DeviceType;
import org.openhab.io.hueemulation.internal.dto.HueGroupEntry;
@@ -56,7 +57,7 @@ public class LightsAndGroupsTests {
LightsAndGroups subject = new LightsAndGroups();
@Before
@BeforeEach
public void setUp() throws IOException {
commonSetup = new CommonSetup(false);
itemRegistry = new DummyItemRegistry();
@@ -81,7 +82,7 @@ public class LightsAndGroupsTests {
commonSetup.start(new ResourceConfig().registerInstances(subject));
}
@After
@AfterEach
public void tearDown() {
commonSetup.dispose();
}

View File

@@ -13,7 +13,8 @@
package org.openhab.io.hueemulation.internal.rest;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.lang.reflect.Type;
@@ -25,6 +26,15 @@ import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.core.automation.Condition;
import org.openhab.core.automation.Rule;
import org.openhab.core.automation.RuleRegistry;
import org.openhab.core.automation.Trigger;
import org.openhab.core.automation.util.RuleBuilder;
import org.openhab.core.items.GenericItem;
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.library.items.ColorItem;
@@ -32,15 +42,6 @@ import org.openhab.core.library.items.SwitchItem;
import org.openhab.core.library.types.HSBType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.types.State;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openhab.core.automation.Condition;
import org.openhab.core.automation.Rule;
import org.openhab.core.automation.RuleRegistry;
import org.openhab.core.automation.Trigger;
import org.openhab.core.automation.util.RuleBuilder;
import org.openhab.io.hueemulation.internal.ConfigStore;
import org.openhab.io.hueemulation.internal.RuleUtils;
import org.openhab.io.hueemulation.internal.dto.HueRuleEntry;
@@ -75,7 +76,7 @@ public class RulesTests {
itemRegistry.add(item);
}
@Before
@BeforeEach
public void setUp() throws IOException {
commonSetup = new CommonSetup(false);
this.cs = commonSetup.cs;
@@ -104,7 +105,7 @@ public class RulesTests {
commonSetup.start(new ResourceConfig().registerInstances(subject));
}
@After
@AfterEach
public void tearDown() {
RuleUtils.random = new Random();
commonSetup.dispose();

View File

@@ -13,7 +13,8 @@
package org.openhab.io.hueemulation.internal.rest;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.lang.reflect.Type;
@@ -24,6 +25,14 @@ import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.glassfish.jersey.server.ResourceConfig;
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.core.automation.Rule;
import org.openhab.core.automation.RuleRegistry;
import org.openhab.core.automation.util.RuleBuilder;
import org.openhab.core.items.GenericItem;
import org.openhab.core.items.GroupItem;
import org.openhab.core.items.ItemRegistry;
@@ -36,14 +45,6 @@ import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;
import org.glassfish.jersey.server.ResourceConfig;
import org.hamcrest.CoreMatchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openhab.core.automation.Rule;
import org.openhab.core.automation.RuleRegistry;
import org.openhab.core.automation.util.RuleBuilder;
import org.openhab.io.hueemulation.internal.ConfigStore;
import org.openhab.io.hueemulation.internal.dto.HueSceneEntry;
import org.openhab.io.hueemulation.internal.rest.mocks.DummyItemRegistry;
@@ -72,7 +73,7 @@ public class SceneTests {
itemRegistry.add(item);
}
@Before
@BeforeEach
public void setUp() throws IOException {
commonSetup = new CommonSetup(false);
this.cs = commonSetup.cs;
@@ -97,7 +98,7 @@ public class SceneTests {
commonSetup.start(new ResourceConfig().registerInstances(subject));
}
@After
@AfterEach
public void tearDown() {
commonSetup.dispose();
}

View File

@@ -13,7 +13,8 @@
package org.openhab.io.hueemulation.internal.rest;
import static org.hamcrest.CoreMatchers.*;
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.*;
@@ -28,19 +29,19 @@ import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.library.items.ColorItem;
import org.openhab.core.library.items.SwitchItem;
import org.glassfish.jersey.server.ResourceConfig;
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.openhab.core.automation.Rule;
import org.openhab.core.automation.RuleManager;
import org.openhab.core.automation.RuleRegistry;
import org.openhab.core.automation.Trigger;
import org.openhab.core.automation.util.RuleBuilder;
import org.openhab.core.automation.util.TriggerBuilder;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.library.items.ColorItem;
import org.openhab.core.library.items.SwitchItem;
import org.openhab.io.hueemulation.internal.ConfigStore;
import org.openhab.io.hueemulation.internal.DeviceType;
import org.openhab.io.hueemulation.internal.RuleUtils;
@@ -66,7 +67,7 @@ public class ScheduleTests {
Schedules subject = new Schedules();
@Before
@BeforeEach
public void setUp() throws IOException {
commonSetup = new CommonSetup(false);
this.cs = commonSetup.cs;
@@ -97,7 +98,7 @@ public class ScheduleTests {
RuleUtils.random = random;
}
@After
@AfterEach
public void tearDown() {
RuleUtils.random = new Random();
commonSetup.dispose();

View File

@@ -13,7 +13,8 @@
package org.openhab.io.hueemulation.internal.rest;
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 java.io.IOException;
@@ -21,6 +22,10 @@ import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.core.items.GenericItem;
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.library.items.ColorItem;
@@ -35,10 +40,6 @@ import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.OpenClosedType;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.types.State;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openhab.io.hueemulation.internal.ConfigStore;
import org.openhab.io.hueemulation.internal.rest.mocks.DummyItemRegistry;
@@ -61,7 +62,7 @@ public class SensorTests {
itemRegistry.add(item);
}
@Before
@BeforeEach
public void setUp() throws IOException {
commonSetup = new CommonSetup(false);
itemRegistry = new DummyItemRegistry();
@@ -84,7 +85,7 @@ public class SensorTests {
commonSetup.start(new ResourceConfig().registerInstances(subject));
}
@After
@AfterEach
public void tearDown() {
commonSetup.dispose();
}

View File

@@ -13,7 +13,8 @@
package org.openhab.io.hueemulation.internal.rest;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.util.Collections;
@@ -23,9 +24,9 @@ import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.server.ResourceConfig;
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.Mockito;
import org.openhab.io.hueemulation.internal.ConfigStore;
import org.openhab.io.hueemulation.internal.HueEmulationConfig;
@@ -49,7 +50,7 @@ public class UsersAndConfigTests {
CommonSetup commonSetup;
@Before
@BeforeEach
public void setUp() throws IOException {
commonSetup = new CommonSetup(false);
@@ -60,7 +61,7 @@ public class UsersAndConfigTests {
commonSetup.start(new ResourceConfig().registerInstances(configurationAccess));
}
@After
@AfterEach
public void tearDown() {
commonSetup.dispose();
}

View File

@@ -13,7 +13,8 @@
package org.openhab.io.hueemulation.internal.upnp;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import java.io.IOException;
@@ -31,11 +32,11 @@ import org.glassfish.grizzly.osgi.httpservice.OSGiMainHandler;
import org.glassfish.grizzly.osgi.httpservice.util.Logger;
import org.glassfish.jersey.server.ResourceConfig;
import org.hamcrest.CoreMatchers;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.openhab.io.hueemulation.internal.rest.CommonSetup;
@@ -57,7 +58,7 @@ public class UpnpTests {
LightsAndGroups lightsAndGroups = new LightsAndGroups();
@BeforeClass
@BeforeAll
public static void setupHttpParts() throws IOException {
commonSetup = new CommonSetup(true);
commonSetup.start(new ResourceConfig());
@@ -72,7 +73,7 @@ public class UpnpTests {
httpServiceImpl = new HttpServiceImpl(mock(Bundle.class), logger);
}
@Before
@BeforeEach
public void setup() {
Executor executor = mock(Executor.class);
Mockito.doAnswer(a -> {
@@ -87,12 +88,12 @@ public class UpnpTests {
subject.overwriteReadyToFalse = false;
}
@After
@AfterEach
public void tearDown() {
subject.deactivate();
}
@AfterClass
@AfterAll
public static void tearDownHttp() {
mainHttpHandler.unregisterAll();
commonSetup.dispose();