0

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

4

1 回答 1

1

弄清楚了。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));
    }

}
于 2010-12-24T19:52:22.543 回答