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:

  1. right click project -> maven -> update project.
  2. mvn clean, try mvn install again.
  3. if didn't fix it, @ import statements , make sure correct classes.
  4. 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

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -