簡體   English   中英

JUnit ExpectedException規則不起作用

[英]JUnit ExpectedException rule doesn't work

我有一種方法應該測試:

public void createNode(String name, String primaryNodeType, String[] mixinNodeTypes) throws RepositoryException {
    final Node parentNode = this.parentNodeStack.peek();
    boolean isParentImport = (name == null && isParentNodeImport);
    if (name == null) {
        if (this.parentNodeStack.size() > 1) {
            throw new RepositoryException("Node needs to have a name.");
        }
        name = this.defaultName;
    }
    //other stuff
}

在我的測試中,我將方法參數放入哪個方法應該拋出RepositoryException(第6行)中。

@RunWith(JMock.class)
public class DefaultContentCreatorTest {

    static final String DEFAULT_NAME = "default-name";
    final Mockery mockery = new JUnit4Mockery();
    DefaultContentCreator contentCreator;

    Session session;
    Node parentNode;
    Property prop;

    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Before
    public void setup() throws Exception {
        final SlingRepository repo = RepositoryProvider.instance().getRepository();
        session = repo.loginAdministrative(null);
        contentCreator = new DefaultContentCreator(null);
        contentCreator.init(U.createImportOptions(true, true, true, false, false),
                new HashMap<String, ContentReader>(), null, null);
        parentNode = session.getRootNode().addNode(getClass().getSimpleName()).addNode(uniqueId());
    }

    @After
    public void cleanup() throws RepositoryException {
        if(session != null) {
            session.save();
            session.logout();
            session = null;
        }
    }

    @Test
    public void createNodeWithoutNameAndTwoInStack() throws RepositoryException {
        contentCreator.init(U.createImportOptions(true, true, true, false, false),
                new HashMap<String, ContentReader>(), null, null);
        //Making parentNodeStack.size() == 1
        contentCreator.prepareParsing(parentNode, DEFAULT_NAME);
        //Making parentNodeStack.size() == 2
        contentCreator.createNode(uniqueId(), null, null);

        //contentCreator.createNode method should now throw an exception
        thrown.expect(RepositoryException.class); //Doesn't work
        thrown.expectMessage("Node needs to have a name."); //Doesn't work
        contentCreator.createNode(null, null, null);
    }
}

但是,正是由於這個RepositoryException測試失敗。 我做錯了什么? 順便說一句,如果我使用@Test(expected = RepositoryException.class)一切正常。

UPD:我正在為Apache Sling類之一編寫單元測試。 您可以在這里查看此類UPD2:有一個失敗的測試異常堆棧跟蹤:

javax.jcr.RepositoryException: Node needs to have a name.
at org.apache.sling.jcr.contentloader.internal.DefaultContentCreator.createNode(DefaultContentCreator.java:225)
at org.apache.sling.jcr.contentloader.internal.DefaultContentCreatorTest.createNodeWithoutNameAndTwoInStack(DefaultContentCreatorTest.java:278)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
at org.jmock.integration.junit4.JMock$1.invoke(JMock.java:37)
at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:107)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:88)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:96)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:100)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:61)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:54)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:52)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

如您所見,它在第278行拋出異常,該行是contentCreator.createNode(null, null, null); 方法調用。

只是為了總結評論中的討論:解決方案是刪除JMock jUnit

嘗試將您的thrown.expect放在您希望從中引發異常的實際調用之前

@Test
public void createNodeWithoutNameAndTwoInStack() throws RepositoryException {
    thrown.expect(RepositoryException.class); //Doesn't work
    thrown.expectMessage("Node needs to have a name."); //Doesn't work 

    contentCreator.init(U.createImportOptions(true, true, true, false, false),
            new HashMap<String, ContentReader>(), null, null);

    //Making parentNodeStack.size() == 1
    contentCreator.prepareParsing(parentNode, DEFAULT_NAME);

    //Making parentNodeStack.size() == 2
    contentCreator.createNode(uniqueId(), null, null);

    // method should now throw an exception
    contentCreator.createNode(null, null, null);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM