Java 春天@ContextConfiguration
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23618039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring @ContextConfiguration
提问by darkZone
I am running the next test:
我正在运行下一个测试:
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/META-INF/spring/applicationContext.xml" })
public class FloorServiceTest {
@Autowired
private FloorService floorService;
@Test
public void testFloorService() {
floorService.findById((long)1);
}
}
With the file applicationContext.xml
under the folder /APP/src/main/resources/META-INF/spring/
与文件applicationContext.xml
夹下的文件/APP/src/main/resources/META-INF/spring/
But it seems not to Autowire correctly the bean:
但似乎没有正确自动装配 bean:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.confloorapp.services.FloorServiceTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.confloorapp.services.FloorService com.confloorapp.services.FloorServiceTest.floorService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.confloorapp.services.FloorService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
回答by ndrone
Try
尝试
@ContextConfiguration(locations = { "classpath:/META-INF/spring/applicationContext.xml" })
Honestly I would step away from the xml and go this route. Change
老实说,我会远离 xml 并走这条路。改变
@ContextConfiguration(locations = { "/META-INF/spring/applicationContext.xml" })
to
到
@ContextConfiguration(classes = { FloorServiceTestConfig.class })
And create the about class
并创建 about 类
@Configuration
public class FloorServiceTestConfig
{
@Bean
public FloorService floorService()
{
return new FloorService();
}
}
This way when you need to mock your beans for class you're not testing it looks like below
这样,当您需要为类模拟 bean 时,您无需测试它,如下所示
@Configuration
public class FloorServiceTestConfig
{
@Bean
public FloorService floorService()
{
return Mockito.mock(FloorService.class);
}
}
回答by Shweta Gulati
The idea of @Configuration
given above works nice. But for that you have to annotate your classes with @Configuration
.
This is not a nice idea when it comes to testing offline i.e when in your organisation test cases are not executed at built time.
In that case going by
@ContextConfiguration(locations = { "classpath:/META-INF/spring/applicationContext.xml" })
will be a nice idea.
@Configuration
上面给出的想法很好。但是为此,您必须使用@Configuration
. 当涉及到离线测试时,即在您的组织中测试用例不在构建时执行时,这不是一个好主意。在这种情况下,过去
@ContextConfiguration(locations = { "classpath:/META-INF/spring/applicationContext.xml" })
将是一个好主意。
For using this method, take care of the following:
要使用此方法,请注意以下事项:
Check your classpath in
.classpath
file of your project.If you cannot write the path of
applicationContext
relative to classpath.
检查
.classpath
项目文件中的类路径。如果你不能写
applicationContext
相对于类路径的路径。
You can have one more file as applicationContextTest
folder kept in your test case folder and then one can use
@ContextConfiguration(locations = { "classpath:applicationContextTest.xml"})
您可以将另一个文件作为applicationContextTest
文件夹保存在测试用例文件夹中,然后可以使用
@ContextConfiguration(locations = { "classpath:applicationContextTest.xml"})
回答by O.Badr
In your case you should use:
在您的情况下,您应该使用:
@ContextConfiguration(locations = "file:src/main/resources/META-INF/spring/applicationContext.xml")
Another hint, I see that you use your production applicationContext.xml
in your test environment, IMHO it's not a good practice, try use a specific one for testing like applicationContext-test.xml
, so you can play with your test context without hurting your production environment.
另一个提示,我看到您applicationContext.xml
在测试环境中使用您的产品,恕我直言,这不是一个好习惯,请尝试使用特定的测试环境,例如applicationContext-test.xml
,这样您就可以在不损害生产环境的情况下使用测试上下文。