JDepend Dependency Constraint Failing

644 views Asked by At

I'm using JDepend to analyze my architecture and create structural tests to verify dependency within a Layered architecture. The two relevant layers are com.domain and com.infrastructure. Domain concretely depends on the Infrastructure layer.

Why is the following test failing?

import java.io.IOException;
import jdepend.framework.DependencyConstraint;
import jdepend.framework.JDepend;
import jdepend.framework.JavaPackage;
import junit.framework.TestCase;

public class DependencyTest extends TestCase {

    private JDepend jdepend;

    @Override
    public void setUp() throws IOException {
        jdepend = new JDepend();

        jdepend.addDirectory("build/classes/com");
    }

    public void testDomainDependsOnInfastructure_ShouldBeTrue() {
        DependencyConstraint constraint = new DependencyConstraint();

        JavaPackage domainPackage = constraint.addPackage("com.domain");
        JavaPackage infastructurePackage = constraint.addPackage("com.infrastructure");

        domainPackage.dependsUpon(infastructurePackage);
        jdepend.analyze();

        assertEquals("Domain doesn't depend on Infrastructure layer", true, jdepend.dependencyMatch(constraint));
    }

}

jdepend.analyze() returns the relevant packages, so I do know it is finding my code. Any ideas?

1

There are 1 answers

1
Mike On BEST ANSWER

Figured it out. JDepend's match function checks all packages, including libraries. I had to custom load it with just the packages I wanted. Here's the code that resolved my problem, if anyone ever runs into this problem.

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import jdepend.framework.DependencyConstraint;
import jdepend.framework.JDepend;
import jdepend.framework.JavaPackage;
import junit.framework.TestCase;

public class DependencyTest extends TestCase {

    private JDepend jdepend;

    @Override
    public void setUp() throws IOException {
        jdepend = new JDepend();
        jdepend.addDirectory("build/classes/com");
    }

    public void testDomainDependsOnInfastructure_ShouldBeTrue() {
        DependencyConstraint constraint = new DependencyConstraint();

        JavaPackage distribution = constraint.addPackage("com.distribution");
        JavaPackage domainPackage = constraint.addPackage("com.domain");
        JavaPackage infastructurePackage = constraint.addPackage("com.infrastructure");

        distribution.dependsUpon(domainPackage);
        domainPackage.dependsUpon(infastructurePackage);
        jdepend.analyze();

        Collection<JavaPackage> actual = new ArrayList<JavaPackage>();
        actual.add(domainPackage);
        actual.add(distribution);
        actual.add(infastructurePackage);

        assertEquals("Domain doesn't depend on Infrastructure layer", true, constraint.match(actual));
    }

}