Spring @ContextConfiguration

12

我正在运行下一个测试:

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);

    }
}

在文件夹/APP/src/main/resources/META-INF/spring/下有一个名为applicationContext.xml的文件。

但是似乎无法正确自动装配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)}

发布应用程序上下文? - chrylis -cautiouslyoptimistic-
请问您能否发布您的ApplicationContext文件。尝试在您的类中使用ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/META-INF/spring/applicationContext.xml");,这可能会起作用。 - RIP SunMicrosystem
你可以尝试使用:@ContextConfiguration(locations = { "classpath:/applicationContext.xml" }) - Arpit Aggarwal
3个回答

18
@ContextConfiguration(locations = { "classpath:/META-INF/spring/applicationContext.xml" })

说实话,我会放弃使用XML而选择这种方式。

@ContextConfiguration(locations = { "/META-INF/spring/applicationContext.xml" })
@ContextConfiguration(classes = { FloorServiceTestConfig.class })

创建关于类

@Configuration
public class FloorServiceTestConfig
{
    @Bean
    public FloorService floorService()
    {
          return new FloorService();
    }
}

这样,当你需要模拟那些你不想测试的类的 bean 时,代码看起来应该是这个样子:

@Configuration
public class FloorServiceTestConfig
{
    @Bean
    public FloorService floorService()
    {
          return Mockito.mock(FloorService.class);
    }
}

@ContextConfiguration(locations = { "classpath:/META-INF/spring/applicationContext.xml" }) 没有起作用 :-( - darkZone

1
上面给出的@Configuration的想法很好。但是为了做到这一点,您必须使用@Configuration注释您的类。 当在组织中测试用例不在构建时间执行时,这并不是一个好主意。 在这种情况下,按照 @ContextConfiguration(locations = { "classpath:/META-INF/spring/applicationContext.xml" })是个好主意。
要使用此方法,请注意以下事项:
  1. 检查项目的.classpath文件中的类路径。

  2. 如果无法将applicationContext的路径写成相对于类路径的形式。

您可以再添加一个名为applicationContextTest的文件夹,并将其放在测试用例文件夹中,然后可以使用 @ContextConfiguration(locations = { "classpath:applicationContextTest.xml"})

0

在您的情况下,您应该使用:

@ContextConfiguration(locations = "file:src/main/resources/META-INF/spring/applicationContext.xml")

另一个提示,我看到你在测试环境中使用了生产applicationContext.xml,在我看来这不是一个好的做法,尝试使用一个特定的测试文件,比如applicationContext-test.xml,这样你就可以在不影响生产环境的情况下玩弄你的测试环境。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接