类加载器问题:即使在测试中明确捕获也未捕获到异常 [英] Classloader issues: Exception not caught even if explicitly caught in test

查看:143
本文介绍了类加载器问题:即使在测试中明确捕获也未捕获到异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是测试场景:

Plugin A具有实用程序类A.xyz(),该类提供了引发java.util.NoSuchtElementException

Plugin A has a utility Class A.xyz() that provides a method that throws a java.util.NoSuchtElementException

Plugin B提供功能".

Fragment F,它使用B作为主机并为B提供测试.

Fragment F which uses B as host and provides tests for B.

现在,我的JUnit测试如下:

Now, my JUnit test looks like this:

try {
    A.xyz(paramTriggeringNoSuchMethodException);
    fail('A.xyz did not throw NoSuchElementException');
} catch (NoSuchElementException e) {
    // expected
}

因此,我希望A.xyz()抛出NoSuchElementException并明确捕获此异常,但是测试仍然失败,告诉我有一个NoSuchtElementException(我刚刚抓住了自己).

So, I expect A.xyz() to throw NoSuchElementException and catch this exception excplicitly, but still the test fails telling me there was a NoSuchtElementException (which I just caught myself).

如果我抓住Throwable而不是NoSuchElementException,则测试将通过.

If I catch Throwable instead of NoSuchElementException, the test will pass.

鉴于所有插件/片段都在同一环境中运行,那怎么可能? 似乎A.xyz()会抛出一个NoSuchElementException,而该NoSuchElementException是使用其他Classloader作为测试本身加载的.

How is that possible given that all plugins/fragments run in the same environment? It seems that A.xyz() throws a NoSuchElementException which was loaded using a different Classloader as the test itself.

BTW:作为插件测试启动时,该测试在Eclipse中运行,但是使用mvn install

BTW: the test runs within Eclipse when started as a plugin test, but fails when run from maven using mvn install

推荐答案

我已经看到,当m2e中的Maven落后时,也会发生类似的事情.我将尝试通过以下方法解决该问题:

I've seen similar things happen when Maven in m2e gets behind. I would try the following things to fix the problem:

  1. 右键单击项目-> Maven->更新项目.
  2. mvn clean,然后再次尝试.
  3. 如果这不能解决问题,请查看您的import语句,并确保它们是正确的类.
  4. 如果这些方法不能解决问题,请实际检查throwable.getClass().getName()throwable.getClass().getClassLoader()的输出,并查看它们在Maven JUnit和Eclipse JUnit中是否返回相同的输出.
  1. Right click Project -> Maven -> Update Project.
  2. mvn clean, then try mvn install again.
  3. If that didn't fix it, look at your import statements and make sure they are the correct classes.
  4. If those don't fix the problem, actually check the output of throwable.getClass().getName() and throwable.getClass().getClassLoader() and see if they return the same output in both the Maven JUnit and Eclipse JUnit.


顺便说一句,为什么在JUnit测试中有一个try-catch块?相反,我会这样做:


As an aside, why do you have a try-catch block in your JUnit test? I would instead do:

@Test(expected=NoSuchElementException.class) {
public void testNoSuchElement() {
    A.xyz(paramTriggeringNoSuchMethodException);
}

此处是Javadoc此参数@Test.expected()

预期

public abstract Class<? extends Throwable> expected

(可选)指定expected(Throwable),以使测试方法成功,前提是该方法引发了指定类的异常.

Optionally specify expected, a Throwable, to cause a test method to succeed iff an exception of the specified class is thrown by the method.

默认:

org.junit.Test.None.class

这篇关于类加载器问题:即使在测试中明确捕获也未捕获到异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆