Spring测试和Maven

3
我是一名有用的助手,可以为您翻译文本。

我正在尝试测试我的Spring Web应用程序,但遇到了一些问题。

我在我的Maven项目中添加了一个测试类。

@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration
public class UserServiceTest {

    @Autowired
    private UserService userService;

    @Test
    public void testName() throws Exception {
        List<UserEntity> userEntities = userService.getAllUsers();

        Assert.assertNotNull(userEntities);
    }
}

但是当我尝试运行这个测试时,userService出现了NullPointerException。 IntelliJ显示“无法自动装配。找不到UserService类型的bean。” 在添加@RunWith(SpringJUnit4ClassRunner.class)之后,我得到了这个异常。

java.lang.IllegalStateException: Neither GenericXmlContextLoader nor AnnotationConfigContextLoader was able to detect defaults, and no ApplicationContextInitializers were declared for context configuration

我该怎么解决这个问题?我认为需要在我的tomcat服务器上运行这个测试,但是我该如何通过IntelliJ进行部署以进行测试(例如命令“mvn clean install tomcat7:run-war-only”)。


1
不,你不需要在Tomcat上运行此测试,你需要告诉它要加载哪个配置文件/类。 - M. Deinum
3个回答

5

在测试开始之前,您必须提供要初始化的Spring上下文文件的位置。

测试类

@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration(locations = { "classpath:META-INF/your-spring-context.xml" })
public class UserServiceTest extends AbstractJUnit4SpringContextTests {

    @Autowired
    private UserService userService;

    @Test
    public void testName() throws Exception {
        List<UserEntity> userEntities = userService.getAllUsers();

        Assert.assertNotNull(userEntities);
    }
}

your-spring-context.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    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
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean id="userService" class="java.package.UserServiceImpl"/>

</beans>

我的Spring上下文在WEB-INF文件夹中,我能从这里访问它吗? - alvinmeimoun
Spring上下文应该放在src/main/resources或src/test/resources中..但是你可以在这里看到一个关于web-inf文件夹的解决方案:https://dev59.com/TmUp5IYBdhLWcg3w665C#14946430 - Xstian
你也可以使用 @ContextConfiguration(classes = ...) 样式 FWIW: https://dev59.com/a57ha4cB1Zd3GeqPrfrD#42138468 - rogerdpack

4
根据测试的目的不同,你需要编写单元测试或Spring集成测试。在后一种情况下,你需要更改代码为:
@ContextConfiguration(locations = {"classpath:/applicationContext.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class UserServiceTest {

   @Autowired
   private UserService userService;

    //test whatever
}

当执行这样的集成测试时,您可能希望钩入Spring Profile机制。通过利用它,您将能够重用在生产代码中使用的相同配置,同时有选择地替换某些bean(例如通过将生产数据源与内存数据库交换)。
如果您正在使用Spring 4,则Conditional将为您提供更多灵活性。
我建议您认真阅读Spring测试文档中的这一部分,以了解Spring集成测试可以为您做什么。
你提供的案例可能不需要进行集成测试(更多关于 UserService 实际操作的细节会使情况更加清晰)。
例如,如果 UserService 使用了一个 UserDao 然后对来自该 dao 的结果执行一些自定义逻辑,则使用模拟的 UserDao 创建 UserService 的单元测试是一个不错的选择。
如果你正在使用构造函数注入来提供 dao 给 service,则service的构建就是易如反掌的。如果你正在通过 @Autowired 进行字段注入(如果可以避免应该避免),那么你需要通过反射注入模拟。Spring 的 ReflectionTestUtils 是一个非常好用的工具。
然后,你可以为 UserDao 创建集成测试,以测试其正确从数据库中读取数据的能力。
最后请注意,上述评论与测试运行方式(IDE或Maven)无关。如果配置正确,工具将愉快地运行它。

2

我不确定,但认为您需要向ContextConfiguration注释添加locations属性,您可以在其中指定包含所有bean的xml文件。此外,我猜获取所有用户的操作与数据库相关,因此最好添加TransactionConfiguration注释。因此,您应该有这样的代码:

@ContextConfiguration(locations = {"classpath:spring-config-junit.xml"}) @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)


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