我使用seam框架来开发一个web站点。我想在开发时间时分享一个问题。我有一个成功的单缝测试方法;

    @Test
    public void saveTwoCategoryToRoot() throws Exception {
    new FacesRequest() {

        Category enduringRoot = new Category(
                ItemTreeVocabulary.ROOTNODE_NAME);

        protected void beforeRequest() {
            // clear database..
            clearDb();

            enduringRoot.addChild(new Category(
                    ItemTreeVocabulary.COMPUTER));

            enduringRoot.addChild(new Category(
                    ItemTreeVocabulary.WHITE_GOODS));
        };

        protected void invokeApplication() throws Exception {

            // set the root to database..
            ((EntityManager) getValue("#{entityManager}")).persist(enduringRoot);

            // feth the root from database..
            @SuppressWarnings("rawtypes")
            List resultList = ((EntityManager) getValue("#{entityManager}")).createQuery(
                    "Select i From Item i").getResultList();

            Category root = (Category) resultList.get(0);

            // list shuld not to be empty..
            Assert.assertFalse(resultList.isEmpty());

            // checks the nodes in the first depth..
            **Assert.assertEquals(2, root.getChildren().size());**

            // assert rootNode name..
            Assert.assertEquals(((Category) resultList.get(0)).getName(),
                    ItemTreeVocabulary.ROOTNODE_NAME);
            // assert first node in first depth..
            Assert.assertEquals(((Category) resultList.get(0))
                    .getChildren().get(0).getName(),
                    ItemTreeVocabulary.COMPUTER);
            // assert second node in first depth..
            Assert.assertEquals(root
                    .getChildren().get(0).getName(),
                    ItemTreeVocabulary.WHITE_GOODS);
        };

    }.run();

}

类别类别为;
@Entity
public class Category extends Item implements Serializable {

private static final long serialVersionUID = -1154500438874768209L;

private List<Item> children;

public Category() {
}

public Category(String name) {
    this();
    setName(name);
}

@OneToMany(targetEntity=Item.class, mappedBy = "parent",cascade = CascadeType.ALL)
public List<Item> getChildren() {
    return children;
}

public void addChild(Item child) {
    if (children == null) {
        children = new ArrayList<Item>();
    }

    if (!children.contains(child)) {
        child.setParent(this);
        children.add(child);
    }
}

public void setChildren(List<Item> children) {
    this.children = children;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Category other = (Category) obj;
    if (children == null) {
        if (other.children != null)
            return false;
    } else if (!children.equals(other.children))
        return false;
    return true;
}

}
物品类别为;
@Inheritance(strategy = InheritanceType.JOINED)
@Entity
@Table(uniqueConstraints = { @UniqueConstraint(columnNames = "name") })
public class Item implements Serializable {

private static final long serialVersionUID = 8332532835833777840L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
    return id;
}

/**
 * Hold the item's parent.
 */
private Category parent;

/**
 * Hold the item's name.
 */
private String name;

private Long id;

@Transient
public List<Item> getChildren() {
    return null;
}

@ManyToOne(targetEntity=Category.class)
public Category getParent() {
    return parent;
}

public void setParent(Category parent) {
    this.parent = parent;
}


public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
 }

按照这种方法,当我运行测试时,下面的黑线出现异常;
 FAILED: saveOneCategoryToRoot
java.lang.AssertionError: expected:<2> but was:<1>
at org.junit.Assert.fail(Assert.java:91)
at org.junit.Assert.failNotEquals(Assert.java:645)
at org.junit.Assert.assertEquals(Assert.java:126)
at org.junit.Assert.assertEquals(Assert.java:470)
at org.junit.Assert.assertEquals(Assert.java:454)
at com.galaksiya.kobar.test.itemtree.CategoryTest$2.invokeApplication(CategoryTest.java:105)
at org.jboss.seam.mock.AbstractSeamTest$Request.invokeApplicationPhase(AbstractSeamTest.java:656)
at org.jboss.seam.mock.AbstractSeamTest$Request.emulateJsfLifecycle(AbstractSeamTest.java:605)
at org.jboss.seam.mock.AbstractSeamTest$Request.access$100(AbstractSeamTest.java:177)
at org.jboss.seam.mock.AbstractSeamTest$Request$2.doFilter(AbstractSeamTest.java:505)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
at org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:40)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:90)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:206)
at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388)
at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515)
at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:60)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
at org.jboss.seam.mock.AbstractSeamTest$Request.run(AbstractSeamTest.java:499)
at org.jboss.seam.mock.AbstractSeamTest$FacesRequest.run(AbstractSeamTest.java:872)
at com.galaksiya.kobar.test.itemtree.CategoryTest.saveOneCategoryToRoot(CategoryTest.java:120)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:676)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:845)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1169)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.runWorkers(TestRunner.java:1182)
at org.testng.TestRunner.privateRun(TestRunner.java:761)
at org.testng.TestRunner.run(TestRunner.java:612)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:335)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:330)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:292)
at org.testng.SuiteRunner.run(SuiteRunner.java:241)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1169)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1094)
at org.testng.TestNG.run(TestNG.java:1006)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:107)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:199)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:170)

我不明白为什么“whitegoods”的实例不保存数据库。
提前谢谢..

最佳答案

该问题是由这样一个事实引起的:当您向类别的子项添加项时,您只在子项中尚未包含该项时才执行此操作。范畴的equals方法认为两个范畴如果有相同的子类,即使它们没有相同的名字,也是相等的。所以你们两个类别是相等的。

10-07 18:15