为什么在 Eclipse 中的 Junit 下不抛出此异常?
:
我对在 Eclipse 下进行 junit 测试时遇到的一个问题感到摸不着头脑,特别是 基本上,我有一个 junit 4 测试类,初始化方法注释为 @BeforeClass 尝试建立数据库连接,这实际上调用:
try {
return DriverManager.getConnection (DB_CONNECTION_URL,
DB_USERNAME,
DB_PASSWORD);
} catch (SQLException ex) {
throw new MyPersistenceException("Error: unable to connect to database");
}
现在,如果我在独立的 main() 中运行代码,当数据库脱机时,会按预期抛出异常。然而,一旦我将它移动到 junit @BeforeClass 中,异常似乎在黑洞中丢失了 - 您仍然可以跟踪到异常(记住,数据库处于脱机状态),但控制台中没有打印任何内容。
我对这种行为感到困惑,并且似乎与 Eclipse 和 Junit 特别有关(相同的单元测试在 NetBean 下运行良好) - 所以看到任何人遇到任何类似问题或有任何想法都是一种盲目的尝试...
谢谢
奥利弗
all
I am scratching my head over a problem I ran into with junit test under Eclipse in particular:
Basically, I have a junit 4 test class, the initialization method annotated with
@BeforeClass tries to set up the DB connection, which essentially calls:
try {
return DriverManager.getConnection (DB_CONNECTION_URL,
DB_USERNAME,
DB_PASSWORD);
} catch (SQLException ex) {
throw new MyPersistenceException("Error: unable to connect to database");
}
Now, if I ran the code in a standalone main(), the exception is thrown as expected when database is offline. However, as soon as I move it into junit @BeforeClass, the exception seems lost in blackhole - you can still trace to the exception (remember, the database is offline), but nothing prints in the console.
I am baffled by the behavior, and seems have something to do with Eclipse and Junit in particular (the same unit test run fine under NetBean) - so it is a kind shot in the dark to see anyone experience any similar problem or got any idea ...
Thanks
Oliver
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这可能是 Eclipse 插件中的一个错误。我已经追踪了 JUnit 4 源代码中的代码。正如您提到的,类 RunBefores 会抛出异常。但是,异常将通过 addFailure 进入 EachTestNotifier 的失败列表。如果插件忽略了某些内容,它可以解释为什么该问题被遗漏。
I think this might be a bug in the Eclipse plugin. I've traced the code in JUnit 4 source. The class RunBefores will throw an exception as you mention. However, the exception will go into the EachTestNotifier's failure list via the addFailure. If the plugin is ignoring something, it could explain why the issue is missed.