Skip to content

Commit

Permalink
Add a basic test suite
Browse files Browse the repository at this point in the history
This puts the infrastructure in place
for adding more tests in the future.
  • Loading branch information
pjonsson committed Dec 31, 2023
1 parent 4bb88f8 commit 52c2ac7
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
30 changes: 30 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id 'application'
id 'jvm-test-suite'
id 'com.diffplug.spotless' version '6.23.3'
id 'net.ltgt.errorprone' version '3.1.0'
}
Expand Down Expand Up @@ -97,6 +98,35 @@ application {
'--enable-preview', '--enable-native-access', 'ALL-UNNAMED']
}

testing {
suites {
configureEach {
dependencies {
implementation project()
// https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api
implementation 'org.junit.jupiter:junit-jupiter-api:5.10.1'
runtimeOnly 'org.junit.platform:junit-platform-launcher'
}
}
test(JvmTestSuite) {
useJUnitJupiter()
testType = TestSuiteType.INTEGRATION_TEST
}
}
}

test {
reports {
junitXml {
outputPerTestCase = true
mergeReruns = true
}
}
testLogging {
events "failed", "passed", "skipped"
}
}

tasks.withType(JavaCompile).configureEach {
// ErrorProne is slow, only enable with ./gradlew build -Perrorprone.
options.errorprone.enabled = project.hasProperty('errorprone')
Expand Down
2 changes: 1 addition & 1 deletion java/org/contikios/cooja/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"Cooja " + Cooja.VERSION + ", Contiki-NG build interface version " + Cooja.CONTIKI_NG_BUILD_VERSION,
"JVM: ${java.version} (${java.vendor} ${java.vm.name} ${java.vm.version})",
"OS: ${os.name} ${os.version} ${os.arch}"}, sortOptions = false, sortSynopsis = false)
class Main {
public class Main {
/**
* Option for specifying look and feel.
*/
Expand Down
38 changes: 38 additions & 0 deletions src/test/java/TestCooja.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.contikios.cooja.Cooja;
import org.contikios.cooja.Main;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class TestCooja {
private final PrintStream stdout = System.out;
private final PrintStream stderr = System.err;
private final ByteArrayOutputStream out = new ByteArrayOutputStream();
private final ByteArrayOutputStream err = new ByteArrayOutputStream();

@BeforeEach
public void setUpStreams() {
out.reset();
err.reset();
System.setOut(new PrintStream(out));
System.setErr(new PrintStream(err));
}

@AfterEach
public void restoreStreams() {
System.setOut(stdout);
System.setErr(stderr);
}

@Test
void testVersion() {
Main.main(new String[] {"--version"});
assertEquals("", err.toString());
assertTrue(out.toString().contains(Cooja.VERSION));
}
}

0 comments on commit 52c2ac7

Please sign in to comment.