java - Classloader issues: Exception not caught even if explicitly caught in test -
this test scenario:
plugin a
has utility class a.xyz()
provides method throws java.util.nosuchtelementexception
plugin b
provides "functionality".
fragment f
uses b
host , provides tests b
.
now, junit test looks this:
try { a.xyz(paramtriggeringnosuchmethodexception); fail('a.xyz did not throw nosuchelementexception'); } catch (nosuchelementexception e) { // expected }
so, expect a.xyz()
throw nosuchelementexception
, catch exception excplicitly, still test fails telling me there nosuchtelementexception
(which caught myself).
if catch throwable
instead of nosuchelementexception
, test pass.
how possible given plugins/fragments run in same environment? seems a.xyz()
throws nosuchelementexception
loaded using different classloader
test itself.
btw: test runs within eclipse when started plugin test, fails when run maven using mvn install
i've seen similar things happen when maven in m2e gets behind. try following things fix problem:
- right click project -> maven -> update project.
mvn clean
, trymvn install
again.- if didn't fix it, @ import statements , make sure correct classes.
- if don't fix problem, check output of
throwable.getclass().getname()
,throwable.getclass().getclassloader()
, see if return same output in both maven junit , eclipse junit.
as aside, why have try-catch
block in junit test? instead do:
@test(expected=nosuchelementexception.class) { public void testnosuchelement() { a.xyz(paramtriggeringnosuchmethodexception); }
here's javadoc on parameter, @test.expected()
expected
public abstract class<? extends throwable> expected
optionally specify
expected
,throwable
, cause test method succeed iff exception of specified class thrown method.default:
org.junit.test.none.class
Comments
Post a Comment