项目添加Maven支持后,单元测试报异常 javax/mail/internet/AddressException 解决

项目添加Maven支持后,运行JUnit单元测试,出现一下错误:

 

ERROR in ch.qos.logback.core.joran.action.AppenderAction - Could not create an Appender of type 
[com.travelsky.tdp.pkgStock.integration.logback.appender.mail.SMTPAppender]. ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type 

com.travelsky.tdp.pkgStock.integration.logback.appender.mail.SMTPAppender
	at ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type com.travelsky.tdp.pkgStock.integration.logback.appender.mail.SMTPAppender
	at 	at ch.qos.logback.core.util.OptionHelper.instantiateByClassName(OptionHelper.java:54)
	at 	......(省略打印信息)
	at 	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/internet/AddressException
	at 	at java.lang.ClassLoader.defineClass1(Native Method)
	at 	.....(省略打印信息)
	at 	... 41 common frames omitted

ERROR in ch.qos.logback.core.joran.spi.Interpreter@192:115 - ActionException in Action for tag [appender] ch.qos.logback.core.joran.spi.ActionException: 

ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type com.travelsky.tdp.pkgStock.integration.logback.appender.mail.SMTPAppender
	at ch.qos.logback.core.joran.spi.ActionException: ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type 

com.travelsky.tdp.pkgStock.integration.logback.appender.mail.SMTPAppender
	at 	at ch.qos.logback.core.joran.action.AppenderAction.begin(AppenderAction.java:82)
	at 	......(省略打印信息)
	at 	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type com.travelsky.tdp.pkgStock.integration.logback.appender.mail.SMTPAppender
	at 	at ch.qos.logback.core.util.OptionHelper.instantiateByClassName(OptionHelper.java:54)
	at 	... 39 common frames omitted
Caused by: java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/internet/AddressException
	at 	at java.lang.ClassLoader.defineClass1(Native Method)
	at 	.....(省略打印信息)
	at 	... 41 common frames omitted

 单元测试失败,查找原因,发现如下问题:

 

1、Maven依赖中使用的 javeee.jar 版本较高,里面包含了 mail相关的接口,和 mail.jar 相冲突;

2、javaee.jar 中包含了 mail相关接口的 实现类;

3、MyEclipse自带的javaee.jar 版本和 MyEclipse本身的版本有关:MyEclipse6.5自带的javaee.jar 中不包含mail相关的接口,而MyEclipse8.0自带的javaee.jar中包含了mail相关的接口,同时包含了接口的实现;

4、项目中直接用到了mail.jar中的类和方法;

此bug出现的原因是由于类冲突,因此去掉冲突的类即可;但是应该怎么选择呢?

分析如下:

a、此项目是Web项目,因此javaee.jar应保留;

b、javaee.jar中包含mail相关接口的实现,因此javaee.jar应保留;

c、项目中直接用到了mail.jar中的接口,因此mail.jar应该保留;

于是陷入两难。

不过,有个比较拙的方法,Maven依赖中不添加 JavaEE 的依赖,使用MyEclipse中自带的JaveEE依赖;同时,如果该依赖中包含mail相关的接口,将其删除即可,至此,问题解决。

不过,有人会问,如果我换了MyEclipse的版本,不是要每次都要修改吗,如果忘记了的话,岂不是白浪费时间嘛.....所以说,该解决方案仅仅是临时性的,比较正统的解决方案如下:

将pom.xml中 <scope>provided</scope> 的依赖放置到 其他依赖的后面即可,而不需要删除任何依赖。

你可能感兴趣的:(java,maven,JUnit,dependency,mail)