1 导入Spring集成Junit的坐标
2 使用@RunWith注解替换原来的运行期
3 使用@ContextConfiguration指定配置文件或配置类
4 使用@Autowired注入需要测试的对象
5 创建测试方法进行测试
package test; import java.sql.SQLException; import javax.sql.DataSource; 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 config.SpringConfiguration; import service.UserService; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") //@ContextConfiguration(classes = {SpringConfiguration.class}) public class SpringJunitTest { @Autowired private UserService userservice; @Autowired private DataSource dataSource; @Test public void test1() throws Exception { userservice.save(); System.out.println(dataSource.getConnection()); } }
标签:集成,RunWith,Autowired,Spring,ContextConfiguration,import,org,Junit From: https://www.cnblogs.com/zlyyds/p/16922703.html