this.getClass()。getClassLoader()。getResource(“...”)和NullPointerException(this.getClass().getClassLoader().getResource(“…”) and NullPointerException)
我在eclipse helios中创建了一个包含单个子模块的最小maven项目。
在src / test / resources文件夹中,我放了一个文件“install.xml”。 在文件夹src / test / java中,我创建了一个包含单个类的单个包:
@Test public void doit() throws Exception { URL url = this.getClass().getClassLoader().getResource("install.xml"); System.out.println(url.getPath()); }
但是当我运行代码作为junit 4单元测试时,我只是得到一个NullPointerException。 以前,这已经有一百万次了。 有任何想法吗?
我遵循了本指南:
http://www.fuyun.org/2009/11/how-to-read-input-files-in-maven-junit/
但仍然得到相同的错误。
I have created a minimal maven project with a single child module in eclipse helios.
In the src/test/resources folder I have put a single file "install.xml". In the folder src/test/java I have created a single package with a single class that does:
@Test public void doit() throws Exception { URL url = this.getClass().getClassLoader().getResource("install.xml"); System.out.println(url.getPath()); }
but when I run the code as a junit 4 unit test I just get a NullPointerException. This has worked fine a million of times before. Any ideas?
I have followed this guide:
http://www.fuyun.org/2009/11/how-to-read-input-files-in-maven-junit/
but still get the same error.
原文:https://stackoverflow.com/questions/3803326
最满意答案
您应该将
env.rb
文件放在initializers
文件夹中。 如果您不想将env.rb
文件推送到heroku,可以将env.rb
文件添加到.gitignore
文件中。You should put the
env.rb
file ininitializers
folder. You can addenv.rb
file to.gitignore
file if you don't want to push it to heroku.
相关问答
更多-
您应该将env.rb文件放在initializers文件夹中。 如果您不想将env.rb文件推送到heroku,可以将env.rb文件添加到.gitignore文件中。 You should put the env.rb file in initializers folder. You can add env.rb file to .gitignore file if you don't want to push it to heroku.
-
在String中展开env变量(Expand env variables in String)[2023-10-10]
基本上,您希望使用环境变量进行字符串插值并展开主目录。 我不知道一个简单的方法来做后者,但如果你使用Spring进行设置,你可以使用它的PropertyPlaceholderConfigurer来替换字符串中的占位符。 默认情况下,环境变量包含在占位符替换集中。 更新:由于这是来自用户,您仍然可以使用Spring辅助类: String stringToBeInterpolated = ....; Properties properties = System.getProperties(); Property ... -
[更新] 虽然“老回答”下的解决方案可以解决一般问题,但本部分是在您的评论澄清后回答您的具体问题。 您应该能够设置环境变量,就像您在问题中指定的一样。 例如,我有一个使用HTTP基本认证的Heroku应用程序。 # app/controllers/application_controller.rb class ApplicationController < ActionController::Base protect_from_forgery before_filter :authenticate ...
-
当然,我自己也倾向于每次都“弯曲”一个框架,总有一种方法,但并不总是最好的解决方案。 我并没有在这里给出一个完整的实现,只是指出你的方向,这可能对你有用。 您可以扩展Laravel的基本应用程序类Illuminate\Foundation\Application ,其中包含在应用程序引导期间加载的$environmentFile变量存储环境文件,或者可能覆盖函数loadEnvironmentFrom($file)或environmentFile() 。 整个逻辑取决于你。 所以基本上你需要做的只是为了能够使 ...
-
如果你的命令只是一个shell命令,你可以在子shell中运行你的命令,如下所示: ( . .env ; echo "$TEST" ) source或. 内置包含空格的赋值没有问题。 它将在当前shell环境中的.env文件中设置变量。 在调用外部程序的可能性更大的情况下,您还必须在env文件中为每个赋值添加“export”,如下所示: export TEST="hello world" 这是必要的,因为source不会像env那样导出指定的变量,即它们仅在子shell中设置,而不是在子shell内启动 ...
-
没有.env文件的命令行生成中的Laravel和环境变量(Laravel and environment variables in command line production without .env file)[2021-11-25]
我一直认为.env文件只是设置服务器的一部分。 git会忽略.env文件,所以一旦设置完毕,你可以推送/拉动你内心的内容,它会单独保留.env文件。 到目前为止,至少我是如何做到的。 I always thought that the .env file was just part of setting up your server. The .env file is ignored by git, so once it's setup, you can push/pull to your heart's ... -
宝石提供了一个发电机: $ rails generate figaro:install 生成器创建一个config / application.yml文件并修改.gitignore文件以防止将文件检入到git存储库中。 您可以将环境变量作为键/值对添加到config / application.yml中 : GMAIL_USERNAME: Your_Username 环境变量将作为ENV变量在您的应用程序中的任何位置可用: ENV["GMAIL_USERNAME"] 这为您提供了在代码中使用相同变量的 ...
-
使用zsh中的.env(dotenv)文件从Ruby访问环境变量(Accessing environment vars from Ruby using a .env (dotenv) file in zsh)[2023-08-01]
与dotenv文档中所述的dotenv ,您实际上需要使用.env文件中的export关键字来使参数可用于环境,例如 export FOO=foo 唯一的例外是,如果参数已经是一个环境变量。 例如,如果它已经在~/.zshrc导出,或者它已经是开始时获得的zsh环境的一部分(例如PATH或HOME )。 当更改到目录时,所有dotenv都会自动获取任何.env文件。 没有额外的“魔法”。 这意味着.env需要是一个有效的zsh脚本,并且其内容在当前shell会话的上下文中运行(基本上就像手动键入它一样)。 ... -
这有一个很棒的宝石,名为rails_config : github repo 但是,如果您只想配置此变量并且只需要此变量,则可以在rails已具有的环境配置文件中创建常量。 就像是: 在config / environments / production.rb中 # production.rb Rails.configuration.my_awesome_changing_domain = "somedomain.me" 在config / environments / development.rb中 # ...
-
cross-env NODE_ENV =测试节点-e \“require('./ setup-env')()\”&& jasmine-ts ** / *。spec.ts 这创建了一个NODE_ENV设置为测试的进程 然后,您创建一个需要'setup-env'的不同进程,然后退出该进程 然后你运行只有NODE_ENV = test设置的jasmine-ts cross-env NODE_ENV=test node -e \"require('./setup-env')()\" && jasmine-ts ** ...