✅ Add @GuiTest annotation for tests that require a GUI
authorDavid Roden <github-a8in@qsheltier.de>
Sun, 11 May 2025 08:42:24 +0000 (10:42 +0200)
committerDavid Roden <github-a8in@qsheltier.de>
Sun, 11 May 2025 08:42:24 +0000 (10:42 +0200)
pom.xml
test/pom.xml [new file with mode: 0644]
test/src/main/java/de/qsheltier/msta/test/GuiTest.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 4508734..934eca5 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                                <version>${project.version}</version>
                        </dependency>
                        <dependency>
+                               <groupId>de.qsheltier</groupId>
+                               <artifactId>msta-test</artifactId>
+                               <version>${project.version}</version>
+                       </dependency>
+                       <dependency>
                                <groupId>org.junit</groupId>
                                <artifactId>junit-bom</artifactId>
                                <version>5.11.3</version>
        <modules>
                <module>server</module>
                <module>client</module>
+               <module>test</module>
        </modules>
 </project>
diff --git a/test/pom.xml b/test/pom.xml
new file mode 100644 (file)
index 0000000..cb296c6
--- /dev/null
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+                xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+       <modelVersion>4.0.0</modelVersion>
+
+       <parent>
+               <groupId>de.qsheltier</groupId>
+               <artifactId>msta</artifactId>
+               <version>0.1-SNAPSHOT</version>
+       </parent>
+
+       <artifactId>msta-test</artifactId>
+
+       <name>MSTA – Test</name>
+       <description>Manual Software Testing Avoidance – Testing Component</description>
+
+       <dependencies>
+               <dependency>
+                       <groupId>de.qsheltier</groupId>
+                       <artifactId>msta-client</artifactId>
+               </dependency>
+               <dependency>
+                       <groupId>org.junit.jupiter</groupId>
+                       <artifactId>junit-jupiter-api</artifactId>
+                       <scope>compile</scope>
+               </dependency>
+               <dependency>
+                       <groupId>org.hamcrest</groupId>
+                       <artifactId>hamcrest</artifactId>
+                       <scope>compile</scope>
+               </dependency>
+       </dependencies>
+
+</project>
diff --git a/test/src/main/java/de/qsheltier/msta/test/GuiTest.java b/test/src/main/java/de/qsheltier/msta/test/GuiTest.java
new file mode 100644 (file)
index 0000000..a817ee8
--- /dev/null
@@ -0,0 +1,42 @@
+package de.qsheltier.msta.test;
+
+import java.awt.GraphicsEnvironment;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.condition.DisabledIf;
+
+/**
+ * Annotation for a test that requires a GUI toolkit to be available.
+ * If no GUI toolkit is available (as per {@link IsHeadless#isHeadless()},
+ * tests annotated with this annotation are skipped.
+ */
+@Inherited
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+@Tag("gui")
+@DisabledIf("de.qsheltier.msta.test.IsHeadless#isHeadless")
+public @interface GuiTest {
+}
+
+/**
+ * Helper for JUnit’s {@link DisabledIf}, to determine if {@link GuiTest}s
+ * should be run.
+ */
+@SuppressWarnings("unused")
+class IsHeadless {
+
+       /**
+        * Returns {@code true} if not GUI toolkit is available.
+        *
+        * @return {@code true} if no GUI toolkit is available,
+        *        {@code false} otherwise
+        */
+       public static boolean isHeadless() {
+               return GraphicsEnvironment.isHeadless();
+       }
+
+}