尽管引发了预期的异常,但jUnit测试失败

这是我的应用程序中的测试方式:

@Test (expected = UniqueFieldValueConstraintViolationException.class)
    public void testName() {
       Branch b1 = new Branch("tetsname","Test 234","06:00-17:00","098144658");
       Branch b2 = new Branch("tetsname","Test 234","06:00-17:00","098144658");
       db.store(b1);
       db.store(b2);
       db.commit();
     }

尽管抛出了异常,但此测试失败:

com.db4o.constraints.UniqueFieldValueConstraintViolationException:class:dataModel.Branch field:pNumber at
com.db4o.constraints.UniqueFieldValueConstraint $ 1.ensureSingleOccurence(UniqueFieldValueConstraint.java:66)at com.db4o.constraints.UniqueFieldValueConstraint $ 1.onEvent(UniqueFieldValueConstraint.java:97)at com.db4o.internal.events.Event4Impl.trigger(Event4Impl . java:78)at com.db4o.internal.events.EventRegistryImpl $ 4.run(EventRegistryImpl.java:123)at com.db4o.foundation.DynamicVariable.with(DynamicVariable.java:54)at com.db4o.internal.InCallback . 运行(InCallback.java:24)com.db4o.internal.events.EventRegistryImpl.withExceptionHandlingInCallback(EventRegistryImpl.java:279)at com.db4o.internal.events.EventRegistryImpl.commitOnStarted(EventRegistryImpl.java:121)at com.db4o .internal.LocalTransaction.dispatchCommittingCallback(LocalTransaction.java:89)at com.db4o.internal.LocalTransaction.commit(LocalTransaction.java:66)at com.db4o.internal.LocalTransaction.commit(LocalTransaction.java:59)at com . db4o.internal.LocalObjectContainer.commitTransaction(LocalObjectContainer.java:689)at at com.db4o.internal.LocalObjectContainer.close2(LocalObjectContainer.java:94)at com.db4o.internal.ObjectContainerBase.close1(ObjectContainerBase.java:361)at com.db4o.internal.ObjectContainerBase.close(ObjectContainerBase.java:344) at tests.BranchTests.cleanUp(BranchTests.java:127)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at at java.lang.reflect.Method.invoke(未知来源)org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:47)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable . java:12)org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)atg.junit.runners.runners.statements.RunAfters.evaluate(RunAfters.java:33)org.junit.runners .parentRunner.runLeaf(ParentRunner.java:271)位于org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4Class) Runner.java:70)org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:238)org.junit.runners.ParentRunner $ 1 .schedule(ParentRunner.java:63)org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53)org.junit.runners org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run上的.garentRunner $ 2.evaluate(ParentRunner.java:229)org.junit.runners.ParentRunner.run(ParentRunner.java:309)(JUnit4TestReference.java: 50)org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)at org位于org.eclipse.jdt的org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)的.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) .internal.junit.runner.RemoteTes tRunner.main(RemoteTestRunner.java:197)

我该如何避免这种行为?

回答(1)

2 years ago

从堆栈跟踪中,您可以看到 testName() 方法中没有抛出此异常:它被 tests.BranchTests.cleanUp 抛出,这可能是在您的实际测试之前或之后运行的单独方法 .

JUnit的 expected 赢了't cover exceptions thrown during these phases. Either ensure these failures don' t发生在这里,或者捕获并忽略它们 .