【发布时间】:2014-11-21 05:56:15
【问题描述】:
我正在尝试测试我的 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')
【问题讨论】:
-
不,你不需要在 tomcat 上运行这个测试,你必须告诉它要加载哪个配置文件/类。
标签: java spring maven intellij-idea junit