[英]Spring jUnit Testing properties file
我有一個 jUnit 測試,它有自己的屬性文件 (application-test.properties) 和它的 spring 配置文件 (application-core-test.xml)。
其中一種方法使用由 spring 配置實例化的 object,這是一個 spring 組件。 類中的一個成員從 application.properties 派生其值,這是我們的主要屬性文件。 通過 jUnit 訪問此值時,它始終為 null。我什至嘗試更改屬性文件以指向實際屬性文件,但這似乎不起作用。
這是我訪問屬性文件 object 的方式
@Component
@PropertySource("classpath:application.properties")
public abstract class A {
@Value("${test.value}")
public String value;
public A(){
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
public A(String text) {
this();
// do something with text and value.. here is where I run into NPE
}
}
public class B extends A {
//addtnl code
private B() {
}
private B(String text) {
super(text)
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:META-INF/spring/application-core-test.xml",
"classpath:META-INF/spring/application-schedule-test.xml"})
@PropertySource("classpath:application-test.properties")
public class TestD {
@Value("${value.works}")
public String valueWorks;
@Test
public void testBlah() {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
B b= new B("blah");
//...addtnl code
}
}
首先,@PropertySource 中的 application.properties 應該讀取application-test.properties
如果這是文件的名稱(匹配這些事情很重要):
@PropertySource("classpath:application-test.properties ")
該文件應該在您的/src/test/resources
類路徑下(在根目錄下)。
我不明白您為什么要將依賴項硬編碼到名為application-test.properties
的文件中。 該組件是否僅用於測試環境?
正常的做法是在不同的類路徑上擁有同名的屬性文件。 您加載一個或另一個取決於您是否正在運行測試。
在典型布局的應用程序中,您將擁有:
src/test/resources/application.properties
和
src/main/resources/application.properties
然后像這樣注入它:
@PropertySource("classpath:application.properties")
更好的做法是在 spring 上下文中將該屬性文件作為 bean 公開,然后將該 bean 注入任何需要它的組件中。 這樣,您的代碼就不會被對 application.properties 的引用所困擾,並且您可以使用任何您想要的東西作為屬性的來源。 這是一個例子: 如何在spring項目中讀取屬性文件?
至於測試,您應該從 Spring 4.1 開始使用,它將覆蓋在其他地方定義的屬性:
@TestPropertySource("classpath:application-test.properties")
測試屬性源的優先級高於從操作系統環境或 Java 系統屬性以及應用程序添加的屬性源(如 @PropertySource)加載的屬性源
我遇到了同樣的問題,花了太多的卡路里來尋找正確的解決方法,直到我決定通過文件閱讀來安定下來:
Properties configProps = new Properties();
InputStream iStream = new ClassPathResource("myapp-test.properties").getInputStream();
InputStream iStream = getConfigFile();
configProps.load(iStream);
如果您在 docker 容器中使用 jar 文件和資源屬性文件,例如application.properties
被打包在包含 java 的同一classes
目錄中(這是 IntelliJ IDE 自動為存儲在/src/main/resources
中的資源文件所做的事情/src/main/resources
),這對我有幫助:
public static Properties props = new Properties();
static {
try {
props.load(
Thread
.currentThread()
.getContextClassLoader()
.getResourceAsStream("application.properties")
);
} catch (Exception e) {
e.printStackTrace();
}
}
大多數其他方法要么只在 IDE 內工作,要么在 Docker 內工作。 這兩者都適用。
if you want to load a few properties, I found a good way in the spring
ReflectionTestUtils.
@Before
Public void setup(){
ReflectionTestUtils.setField(youclassobject, "value", "yourValue")
}
>you can follow this link as well for more details https://roytuts.com/mock-an- autowired-value-field-in-spring-with-junit-mockito/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.