`

getResourceAsStream Returning Null

阅读更多

在单元测试时:

Maven + Junit + getResourceAsStream Returning Null

经过验证:*.properties 在 src/test/main 下,一会能找到,一会找不到。

 

问题解决方法:

在maven pom.xml 中,设置文件字段导入。

  <testResources>
   <testResource>
    <directory>src/test/resources</directory>
    <filtering>true</filtering>
   </testResource>
   <testResource>
    <directory>src/test/java</directory>
    <filtering>true</filtering>
   </testResource>
  </testResources>

 

编译输出:

[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ esi-faces ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 11 resources
[INFO] Copying 26 resources

 

原因:

资源文件没有加载,被maven默认过滤了。

 

分享到:
评论

相关推荐

    getResourceAsStream

    java getResourceAsStream种类,分我所知道的3种情况

    Java中getResourceAsStream用法分析

    主要介绍了Java中getResourceAsStream用法,较为详细的分析了getResourceAsStream的功能及用法,需要的朋友可以参考下

    Java中getResourceAsStream的用法.md

    Java中getResourceAsStream的用法.md

    Java 使用getClass().getResourceAsStream()方法获取资源

    主要介绍了Java 使用getClass().getResourceAsStream()方法获取资源的相关资料,这里主要讲解哪种方式可以获取到文件资源,需要的朋友可以参考下

    DButil.java

    prop.load(DButil.class.getResourceAsStream("/db_config.properties")); info = new DButil(); info.driver = prop.getProperty("driver"); info.dbName = prop.getProperty("dbName"); info.host = prop....

    数据连接池

    InputStream is = getClass().getResourceAsStream("/db.properties"); Properties dbProps = new Properties(); try { dbProps.load(is); } catch (Exception e) { System.err.println("不能读取属性文件. ...

    Aspose.Words.jdk16_c处理word文档,不带水印版本

    is = new Document().getClass().getResourceAsStream("/resources/aspose.word.license.xml"); if(is==null) throw new RuntimeException("Cannot find licenses file. Please contact wdmsyf@yahoo....

    java-loadresource.rar

    Java加载资源文件的两种方法getResource与getResourceAsStream

    读取Properties文件的java类

    is = ReadProperties.class.getClassLoader().getResourceAsStream( strFilePath); if (is == null) { WriteLog.writeLog("閰嶇疆鏂囦欢涓嶅瓨鍦紝璇锋鏌ユ枃浠跺悕鎴栬矾寰勬槸鍚︽纭紒"); ...

    DruidJDBCUtils.java

    InputStream is = DruidJDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"); prop.load(is); ds = DruidDataSourceFactory.createDataSource(prop); } catch (IOException e) { e....

    ISO_v2_300_eci.zip

    ColorSpace cpace = new ICC_ColorSpace(ICC_Profile.getInstance(ThumbnailatorImageUtil.class.getClassLoader().getResourceAsStream("ISOcoated_v2_300_eci.icc"))); ColorConvertOp op = new ...

    jdbc练习的一个工具类

    .getResourceAsStream(DBINFO_FIIE_NAME); try { infos.load(is); } catch (Exception e) { e.printStackTrace(); throw new ExceptionInInitializerError("属性文件加载错误"); } finally { ...

    传智博客jdbc学习案例

    private static UserDao userDao = null; private static DaoFactory instance = new DaoFactory(); private DaoFactory() { try { Properties prop = new Properties(); InputStream inStream = ...

    Java连接数据库代码

    in = MyTask.class.getClassLoader().getResourceAsStream("db.properties"); prop.load(in); this.sql_ip=prop.getProperty("DB_IP").trim(); this.sql_db_name=prop.getProperty("DB_NAME").trim(); this.sql...

    Java简单音乐播放器播放wav文件示例.rar

     InputStream is = this.getClass().getResourceAsStream("/music.wav"); //音乐文件输入流  player = Manager.createPlayer(is,"audio/x-wav"); //实例化播放器  }catch(Exception e){}  }  public void ...

    使用J2SE API读取Properties文件的六种方法

    1。使用java.util.Properties类的load()方法  示例: InputStream in = lnew ...InputStream in = JProperties.class.getResourceAsStream(name);  Properties p = new Properties();  p.load(in);

    j2me手机游戏代码

    is=getClass().getResourceAsStream("/sprite"+toString(sl)+".dat"); readSprite(is); } if(sl==0&&!dlg[0]) message(2); weatherTask=new TimerTask(this,-2); timer.schedule(weatherTask,0,400)...

    利用Java发送邮件(含附件)的例子

    static { try { props.load(ReadPropertity.class.getClassLoader().getResourceAsStream( "utils.properties")); } catch (IOException e1) { e1.printStackTrace(); } } public static String getProperty(String...

    java解析给定url

    InputStream in = getClass().getResourceAsStream("/config.properties"); try { props.load(in); Enumeration en = props.propertyNames(); System.out.print("读取配置文件:"); while(en....

    activiti部署中文乱码解决,亲测有效,替换三个文件

    activiti部署中文乱码解决,亲测有效, 1.activiti-webapp-explorer2\src\main\webapp\editor-app\i18n下en.json 2.activiti-webapp-explorer2\src\main\resources下 activiti-custom-context.xml ...

Global site tag (gtag.js) - Google Analytics