I'm trying to test my Spring web app but i have some problems.
I added a test class in my 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);
}
}
But i got a NullPointerException
on userService when i try to run this test.
IntelliJ say "Could not autowire. No beans of 'UserService' type found.
After adding @RunWith(SpringJUnit4ClassRunner.class)
, i got this exception
java.lang.IllegalStateException: Neither GenericXmlContextLoader nor AnnotationConfigContextLoader was able to detect defaults, and no ApplicationContextInitializers were declared for context configuration
How can i solve it ? And i think i need to run this test on my tomcat server but how can i deploy for testing with IntelliJ ? (like command 'mvn clean install tomcat7:run-war-only')
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…