无法处理上下文配置的位置和 类
Cannot process locations AND classes for context configuration
我写了以下测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:META-INF/dataContext.xml"},classes = Configiuration.class)
@ActiveProfiles("test")
public class CityDaoImplTest {
....
}
我需要使用来自 xml 文件和 java class bur 的配置,当我调用
mvn 测试我在控制台中看到以下内容:
Tests in error:
initializationError(***.CityDaoImplTest): Cannot process locations AND classes for context configuration [ContextConfigurationAttributes@5bb21b69 declaringClass = '***.CityDaoImplTest', classes = '{***.Configiuration}', locations = '{classpath:META-INF/dataContext.xml}', inheritLocations = true, initializers = '{}', inheritInitializers = true, name = [null], contextLoaderClass = 'org.springframework.test.context.ContextLoader']; configure one or the other, but not both.
如何在不重写配置的情况下修复它?
来自Spring Docs:
Prior to Spring 3.1, only path-based resource locations were supported. As of Spring 3.1, context loaders may choose to support either path-based or class-based resources. As of Spring 4.0.4, context loaders may choose to support path-based and class-based resources simultaneously.
但是,spring-test 有一个小警告。它使用基于 AbstractDelegatingSmartContextLoader
的 SmartContextLoader
不幸的是它不是那么聪明 ;)
@Override
public void processContextConfiguration(
final ContextConfigurationAttributes configAttributes) {
Assert.notNull(configAttributes, "configAttributes must not be null");
Assert.isTrue(!(configAttributes.hasLocations() && configAttributes.hasClasses()), String.format(
"Cannot process locations AND classes for context "
+ "configuration %s; configure one or the other, but not both.", configAttributes));
如代码所示,locations 和 classes 不能同时设置。
那么,如何解决这个问题?好吧,一个解决方案是添加一个额外的配置 class,如下所示:
@Configuration
@ImportResource("classpath:META-INF/dataContext.xml")
class TestConfig {
}
并且,在您的测试代码中使用以下内容:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {Configuration.class, TestConfig.class})
@ActiveProfiles("test")
public class CityDaoImplTest { ... }
从技术上讲,这是 重写配置,但您不必更改现有配置,只需添加一个新的 @Configuration
class(然后class 甚至可以与您的测试用例位于同一个文件中)。
即使你迟到了,我也会 post 我的回答只是为了帮助其他会阅读这篇文章的人。
另一种解决方案是将您的配置 class 声明为 dataContext.xml 中的 bean。
您只需:
<bean class="com.packageWhereConfigClassIsPresent.Configuration"/>
希望对大家有所帮助 ;)
我写了以下测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:META-INF/dataContext.xml"},classes = Configiuration.class)
@ActiveProfiles("test")
public class CityDaoImplTest {
....
}
我需要使用来自 xml 文件和 java class bur 的配置,当我调用
mvn 测试我在控制台中看到以下内容:
Tests in error:
initializationError(***.CityDaoImplTest): Cannot process locations AND classes for context configuration [ContextConfigurationAttributes@5bb21b69 declaringClass = '***.CityDaoImplTest', classes = '{***.Configiuration}', locations = '{classpath:META-INF/dataContext.xml}', inheritLocations = true, initializers = '{}', inheritInitializers = true, name = [null], contextLoaderClass = 'org.springframework.test.context.ContextLoader']; configure one or the other, but not both.
如何在不重写配置的情况下修复它?
来自Spring Docs:
Prior to Spring 3.1, only path-based resource locations were supported. As of Spring 3.1, context loaders may choose to support either path-based or class-based resources. As of Spring 4.0.4, context loaders may choose to support path-based and class-based resources simultaneously.
但是,spring-test 有一个小警告。它使用基于 AbstractDelegatingSmartContextLoader
的 SmartContextLoader
不幸的是它不是那么聪明 ;)
@Override
public void processContextConfiguration(
final ContextConfigurationAttributes configAttributes) {
Assert.notNull(configAttributes, "configAttributes must not be null");
Assert.isTrue(!(configAttributes.hasLocations() && configAttributes.hasClasses()), String.format(
"Cannot process locations AND classes for context "
+ "configuration %s; configure one or the other, but not both.", configAttributes));
如代码所示,locations 和 classes 不能同时设置。
那么,如何解决这个问题?好吧,一个解决方案是添加一个额外的配置 class,如下所示:
@Configuration
@ImportResource("classpath:META-INF/dataContext.xml")
class TestConfig {
}
并且,在您的测试代码中使用以下内容:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {Configuration.class, TestConfig.class})
@ActiveProfiles("test")
public class CityDaoImplTest { ... }
从技术上讲,这是 重写配置,但您不必更改现有配置,只需添加一个新的 @Configuration
class(然后class 甚至可以与您的测试用例位于同一个文件中)。
即使你迟到了,我也会 post 我的回答只是为了帮助其他会阅读这篇文章的人。
另一种解决方案是将您的配置 class 声明为 dataContext.xml 中的 bean。
您只需:
<bean class="com.packageWhereConfigClassIsPresent.Configuration"/>
希望对大家有所帮助 ;)