Convert UnitTestUtility to a JUnit 5 MockHardwareExtension (#1153)

Only initialize the HAL once.
This commit is contained in:
Austin Shalit
2018-06-24 03:19:45 -04:00
committed by Peter Johnson
parent b7807bf9d2
commit 50b13d2f36
7 changed files with 18 additions and 31 deletions

View File

@@ -80,6 +80,7 @@ publishing {
test {
useJUnitPlatform()
systemProperty 'junit.jupiter.extensions.autodetection.enabled', 'true'
testLogging {
events "failed"
exceptionFormat "full"

View File

@@ -7,7 +7,6 @@
package edu.wpi.first.wpilibj;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -20,11 +19,6 @@ class CircularBufferTest {
private final double[] m_addLastOut = {342.657, 234.252, 716.126, 132.344, 445.697,
22.727, 421.125, 799.913};
@BeforeAll
static void before() {
UnitTestUtility.setupMockBase();
}
@Test
void addFirstTest() {
CircularBuffer queue = new CircularBuffer(8);

View File

@@ -11,21 +11,27 @@ import java.util.concurrent.TimeUnit;
import com.google.common.base.Stopwatch;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import edu.wpi.first.wpilibj.hal.HAL;
import edu.wpi.first.wpilibj.util.BaseSystemNotInitializedException;
/**
* Utility class for configuring unit tests.
*/
public final class UnitTestUtility {
private UnitTestUtility() {
/* no-op */
public final class MockHardwareExtension implements BeforeAllCallback {
private static ExtensionContext getRoot(ExtensionContext context) {
return context.getParent().map(MockHardwareExtension::getRoot).orElse(context);
}
/**
* Sets up the base system WPILib so that it does not rely on hardware.
*/
public static void setupMockBase() {
@Override
public void beforeAll(ExtensionContext context) throws Exception {
getRoot(context).getStore(Namespace.GLOBAL).getOrComputeIfAbsent("HAL Initalized", key -> {
initializeHardware();
return true;
}, Boolean.class);
}
private void initializeHardware() {
HAL.initialize(500, 0);
try {
// Check to see if this has been setup

View File

@@ -8,7 +8,6 @@
package edu.wpi.first.wpilibj;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -21,11 +20,6 @@ class PIDToleranceTest {
private static final double m_tolerance = 10.0;
private static final double m_range = 200;
@BeforeAll
static void setupClass() {
UnitTestUtility.setupMockBase();
}
private class FakeInput implements PIDSource {
public double m_val;

View File

@@ -13,7 +13,6 @@ import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;
@@ -26,11 +25,6 @@ import static org.junit.jupiter.api.DynamicTest.dynamicTest;
@SuppressWarnings("PMD.AbstractClassWithoutAbstractMethod")
public abstract class UtilityClassTest {
@BeforeAll
static void setup() {
UnitTestUtility.setupMockBase();
}
private final Class m_clazz;
protected UtilityClassTest(Class clazz) {

View File

@@ -9,8 +9,6 @@ package edu.wpi.first.wpilibj.command;
import org.junit.jupiter.api.BeforeEach;
import edu.wpi.first.wpilibj.UnitTestUtility;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -21,7 +19,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
public abstract class AbstractCommandTest {
@BeforeEach
void commandSetup() {
UnitTestUtility.setupMockBase();
Scheduler.getInstance().removeAll();
Scheduler.getInstance().enable();
}

View File

@@ -0,0 +1 @@
edu.wpi.first.wpilibj.MockHardwareExtension