我正在使用JDepend分析我的体系结构并创建结构测试以验证分层体系结构内的依赖性。两个相关的层是com.domain和com.infrastructure。域具体取决于基础架构层。

为什么以下测试失败?

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()返回相关的程序包,所以我知道它正在查找我的代码。有任何想法吗?

最佳答案

弄清楚了。 JDepend的match函数检查所有软件包,包括库。我只需要使用所需的软件包自定义加载它。如果有人遇到此问题,这是解决我问题的代码。

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));
    }

}

09-27 21:17