我有一个我应该测试的方法:
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类编写单元测试。您可以查看此课程here 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);
方法调用的行。
答案 0 :(得分:3)
总结评论中的讨论:解决方案是删除JMock
jUnit
亚军。
答案 1 :(得分:-1)
尝试在您希望从
发出异常的实际调用之前放置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);
}