Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:AKSW RDFUnit RdfUnitJunitRunner

From Leeroopedia
Knowledge Sources
Domains Testing, JUnit, Semantic_Web
Last Updated 2026-10-16 00:00 GMT

Overview

Concrete JUnit 4 custom Runner that integrates RDFUnit RDF validation into JUnit test suites via annotations, auto-generating test cases from schemas and executing them as JUnit tests.

Description

The RdfUnitJunitRunner extends JUnit's `ParentRunner<RdfUnitJunitTestCase>` to provide annotation-driven RDF validation. At construction, it:

1. Reads `@Schema` annotation for the schema ontology URI. 2. Loads the schema model using `RdfReaderFactory.createResourceOrFileOrDereferenceReader()`. 3. Reads optional `@AdditionalData` method for supplementary RDF data. 4. Auto-generates test cases using `RDFUnitTestSuiteGenerator.Builder`. 5. For each `@TestInput` method (returning `RdfReader`), creates one `RdfUnitJunitTestCase` per generated test case.

During execution, `runChild()` wraps each test case in a `ShaclResultStatement` that executes SHACL Lite validation and asserts no violations. The runner validates annotation correctness in `collectInitializationErrors()`: `@Schema` required, at least one `@TestInput` method returning `RdfReader`, at most one `@AdditionalData` method returning `RdfReader`.

Usage

  • JUnit SHACL validation:*** Annotate a test class with `@RunWith(RdfUnitJunitRunner.class)` and `@Schema`, add `@TestInput` methods returning `RdfReader` for input data.
  • CI/CD integration:*** Runs as standard JUnit tests in Maven Surefire, Gradle, or IDE test runners.
  • Multiple test inputs:*** Add multiple `@TestInput` methods to validate different data sets against the same schema.

Code Reference

Source Location

  • Repository: AKSW/RDFUnit
  • File: rdfunit-junit/src/main/java/org/aksw/rdfunit/junit/RdfUnitJunitRunner.java

Signature

public class RdfUnitJunitRunner extends ParentRunner<RdfUnitJunitTestCase> {

    public static final Class<?> INPUT_DATA_RETURN_TYPE = RdfReader.class;

    public RdfUnitJunitRunner(Class<?> testClass) throws InitializationError;

    @Override
    protected List<RdfUnitJunitTestCase> getChildren();

    @Override
    protected Description describeChild(RdfUnitJunitTestCase child);

    @Override
    protected boolean isIgnored(RdfUnitJunitTestCase child);

    @Override
    protected void runChild(RdfUnitJunitTestCase child, RunNotifier notifier);
}

// Annotations
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Schema {
    String uri();
}

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface TestInput {
    String name() default "";
}

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AdditionalData {
}

Import

import org.aksw.rdfunit.junit.RdfUnitJunitRunner;
import org.aksw.rdfunit.junit.Schema;
import org.aksw.rdfunit.junit.TestInput;
import org.aksw.rdfunit.junit.AdditionalData;

I/O Contract

Inputs

Name Type Required Description
testClass Class<?> Yes /// Java class annotated with @RunWith(RdfUnitJunitRunner.class)
@Schema.uri() String Yes Schema ontology URI (resource, file, or dereference)
@TestInput methods Method returning RdfReader Yes One or more methods providing input data
@AdditionalData method Method returning RdfReader No /// Optional supplementary RDF data

Outputs

Name Type Description
JUnit results RunNotifier Pass/fail/ignored notifications per test case
Test descriptions Description Format: [testInputName] testCaseResultMessage

Usage Examples

Basic JUnit SHACL Test

import org.aksw.rdfunit.junit.RdfUnitJunitRunner;
import org.aksw.rdfunit.junit.Schema;
import org.aksw.rdfunit.junit.TestInput;
import org.aksw.rdfunit.io.reader.RdfModelReader;
import org.aksw.rdfunit.io.reader.RdfReader;
import org.aksw.rdfunit.io.reader.RdfReaderFactory;
import org.junit.runner.RunWith;

@RunWith(RdfUnitJunitRunner.class)
@Schema(uri = "/org/aksw/rdfunit/examples/shacl.example.shapes.ttl")
public class MyShaclTest {

    @TestInput
    public RdfReader getInputData() {
        return new RdfModelReader(
            RdfReaderFactory.createResourceReader(
                "/org/aksw/rdfunit/examples/shacl.example.data.ttl").read()
        );
    }
}

With Additional Data

@RunWith(RdfUnitJunitRunner.class)
@Schema(uri = "http://example.org/my-schema#")
public class MyValidationTest {

    @TestInput(name = "valid-data")
    public RdfReader getValidData() {
        return RdfReaderFactory.createDereferenceReader("http://example.org/valid.ttl");
    }

    @TestInput(name = "test-data")
    public RdfReader getTestData() {
        return RdfReaderFactory.createDereferenceReader("http://example.org/test.ttl");
    }

    @AdditionalData
    public RdfReader getExtraTriples() {
        return new RdfModelReader(loadExtraModel());
    }
}

Related Pages

Implements Principle

Requires Environment

Uses Heuristics

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment