这是我的应用程序中测试的样子:
@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) 在 com.db4o.constraints.UniqueFieldValueConstraint $ 1.onEvent(UniqueFieldValueConstraint.java:97) at com.db4o.internal.events.Event4Impl.trigger(Event4Impl.java:78) 在 com.db4o.internal.events.EventRegistryImpl $ 4.run(EventRegistryImpl.java:123) 在com.db4o.foundation.DynamicVariable.with(DynamicVariable.java:54) 在com.db4o.internal.InCallback.run(InCallback.java:24)at com.db4o.internal.events.EventRegistryImpl.withExceptionHandlingInCallback(EventRegistryImpl.java:279) 在 com.db4o.internal.events.EventRegistryImpl.commitOnStarted(EventRegistryImpl.java:121) 在 com.db4o.internal.LocalTransaction.dispatchCommittingCallback(LocalTransaction.java:89) 在 com.db4o.internal.LocalTransaction.commit(LocalTransaction.java:66) 在 com.db4o.internal.LocalTransaction.commit(LocalTransaction.java:59) 在 com.db4o.internal.LocalObjectContainer.commitTransaction(LocalObjectContainer.java:689) 在 com.db4o.internal.LocalObjectContainer.close2(LocalObjectContainer.java:94) 在 com.db4o.internal.ObjectContainerBase.close1(ObjectContainerBase.java:361) 在 com.db4o.internal.ObjectContainerBase.close(ObjectContainerBase.java:344) 在tests.BranchTests.cleanUp(BranchTests.java:127)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at java.lang.reflect.Method.invoke(未知来源)at org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:47) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 在 org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33) 在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:238)at org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:63)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)at at org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53)at at org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229)at at org.junit.runners.ParentRunner.run(ParentRunner.java:309)at at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(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) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
我该如何避免这种行为?
答案 0 :(得分:0)
从堆栈跟踪中,您可以看到testName()
方法中没有抛出此异常:它被tests.BranchTests.cleanUp
抛出,这可能是在您的真实测试之前或之后运行的单独方法
JUnit的expected
不会涵盖这些阶段中抛出的异常。确保这些失败不会在这里发生,或者捕获并忽略它们。