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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:19:51  来源:igfitidea点击:

Spring @ContextConfiguration

javaspringunit-testingspring-mvcjunit4

提问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.xmlunder 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 @Configurationgiven 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:

要使用此方法,请注意以下事项:

  1. Check your classpath in .classpathfile of your project.

  2. If you cannot write the path of applicationContextrelative to classpath.

  1. 检查.classpath项目文件中的类路径。

  2. 如果你不能写applicationContext相对于类路径的路径。

You can have one more file as applicationContextTestfolder 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.xmlin 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,这样您就可以在不损害生产环境的情况下使用测试上下文。