gpt4 book ai didi

java - JDepend 依赖约束失败

转载 作者:行者123 更新时间:2023-12-01 15:58:27 24 4
gpt4 key购买 nike

我正在使用 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 的匹配功能检查所有包,包括库。我必须只使用我想要的包来自定义加载它。如果有人遇到过这个问题,这是解决我的问题的代码。

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

}

关于java - JDepend 依赖约束失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4527971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com