Spring Java 单元测试;我如何在@BeforeClass 处初始化 bean 属性?
Spring Java Unit Testing; How do i initialise a beans property at the point of @BeforeClass?
在测试中class我有一个静态
.
.
.
@BeforeClass
public static void setUpBeforeClass() throws Exception {
//Set utility beans property
// Not a System property.
}
此时,如何初始化或设置 bean 的 属性?
谢谢
如果你想在setUpBeforeClass()方法中设置,这是不可能的。但是如果你只是想设置它并且你正在使用 xml 配置这是另一种方式。
一般来说,您需要创建一个覆盖 applicationContext.xml 的 applicationContext.test.xml。在 JUnit 中使用它而不是 applicationContext.xml
例如这是你的 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<bean name="utilBean" class="UtilBean">
<property name="prop1" value="1"/>
</bean>
<bean name="anotherBean" class="AnotherBean">
<property name="propAnother" value="10"/>
</bean>
</beans>
要在 'utilBean' 中将 'prop1' 设置为 2,您应该像这样创建一个新的 applicationContext.test.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- import the original one -->
<import resource="classpath*:applicationContext.xml" />
<!-- Override utilBean -->
<bean name="utilBean" class="UtilBean">
<!-- Override prop1 value -->
<property name="prop1" value="2"/>
</bean>
</beans>
然后在您的 JUnit class 中,使用 applicationContext.test.xml 而不是 applicationContext.xml
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:applicationContext.test.xml" })
public class JunitTest {
// your tests
}
您的 utilBean 的 prop1 值在您的 JunitTest 中设置为“2”,但在您的原始流程中它将始终为“1”
您还可以在 JUnit 中使用 antoherBean,它的 'propAnother' 值等于 10,无论是在 JUnit 中还是在原始进程中。
抱歉,我没有看到评论...在您的情况下,以这种方式覆盖配置:
@Import(ApplicationContext.class)
@Configuration
public class TestApplicationContext {
...
}
然后覆盖您的 bean。使用此配置 class 而不是 JUnitClass 中的原始配置
在你的测试中 class 声明:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class MyTest {
//... your tests
@Configuration
public static class Config {
//your configuration for the test
}
} // end of test class
在测试中class我有一个静态 . . .
@BeforeClass
public static void setUpBeforeClass() throws Exception {
//Set utility beans property
// Not a System property.
}
此时,如何初始化或设置 bean 的 属性?
谢谢
如果你想在setUpBeforeClass()方法中设置,这是不可能的。但是如果你只是想设置它并且你正在使用 xml 配置这是另一种方式。
一般来说,您需要创建一个覆盖 applicationContext.xml 的 applicationContext.test.xml。在 JUnit 中使用它而不是 applicationContext.xml
例如这是你的 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<bean name="utilBean" class="UtilBean">
<property name="prop1" value="1"/>
</bean>
<bean name="anotherBean" class="AnotherBean">
<property name="propAnother" value="10"/>
</bean>
</beans>
要在 'utilBean' 中将 'prop1' 设置为 2,您应该像这样创建一个新的 applicationContext.test.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- import the original one -->
<import resource="classpath*:applicationContext.xml" />
<!-- Override utilBean -->
<bean name="utilBean" class="UtilBean">
<!-- Override prop1 value -->
<property name="prop1" value="2"/>
</bean>
</beans>
然后在您的 JUnit class 中,使用 applicationContext.test.xml 而不是 applicationContext.xml
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:applicationContext.test.xml" })
public class JunitTest {
// your tests
}
您的 utilBean 的 prop1 值在您的 JunitTest 中设置为“2”,但在您的原始流程中它将始终为“1”
您还可以在 JUnit 中使用 antoherBean,它的 'propAnother' 值等于 10,无论是在 JUnit 中还是在原始进程中。
抱歉,我没有看到评论...在您的情况下,以这种方式覆盖配置:
@Import(ApplicationContext.class)
@Configuration
public class TestApplicationContext {
...
}
然后覆盖您的 bean。使用此配置 class 而不是 JUnitClass 中的原始配置
在你的测试中 class 声明:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class MyTest {
//... your tests
@Configuration
public static class Config {
//your configuration for the test
}
} // end of test class