第一步:在项目导入 spring-test的jar包
(项目引入junit 的jar包 )
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
第二步: 使用 @RunWith注解 和 @ContextConfiguration 注解 集成测试
import com.xucj.api.UserService;
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;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class UserControllerTest {
@Autowired
private UserService userService;
@Test
public void findUserById() throws Exception {
System.out.println(userService.findUserById(1L));
}
}