idea intellij对Spring进行单元测试

1、加入Junit4及SpringJUnit4支持

  1. <!-- junit -->  
  2. <dependency>  
  3.             <groupId>junit</groupId>  
  4.             <artifactId>junit</artifactId>  
  5.             <scope>test</scope>  
  6.         </dependency>  
  7.   
  8. <!-- spring-test -->  
  9.  <dependency>  
  10.         <groupId>org.springframework</groupId>  
  11.         <artifactId>spring-test</artifactId>  
  12.     </dependency>  


2、创建测试类

(wins:右键菜单------->转到(G)------->测试(E) 可快捷在test包,相同目录下建立相应的测试类)

(ios:IntelliJ IDEA提供了一个快捷操作Cmd + Shift + T作为类和测试之间的导航。同时允许用户在那里创建一个测试类。)

3、生成的代码如下:

  1. package net.wll.web.service.impl;  
  2.   
  3. import org.junit.Test;  
  4.   
  5. import static org.junit.Assert.*;  
  6.   
  7. public class DAreasServiceImplTest {  
  8.   
  9.     @Test  
  10.     public void testSelectSubDistricts() throws Exception {  
  11.   
  12.     }  
  13.   
  14.     @Test  
  15.     public void testSelectSubDistricts0() throws Exception {  
  16.   
  17.     }  
  18. }  


4、增加Spring-test支持

    1. package net.xuele.activity.service.impl;  
    2.   
    3. import net.xuele.activity.domain.DAreas;  
    4. import net.xuele.activity.service.DAreasService;  
    5. import org.junit.Test;  
    6. import org.junit.runner.RunWith;  
    7. import org.springframework.test.context.ContextConfiguration;  
    8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
    9. import javax.annotation.Resource;  
    10. import java.util.List;  
    11.   
    12. @RunWith(SpringJUnit4ClassRunner.class)  
    13. /** 注入相关的配置文件:可以写入多个配置文件 **/  
    14. @ContextConfiguration(locations={"classpath:META-INF/spring/application-services.xml",  
    15.         "classpath:META-INF/spring/applicationContext-persist.xml"  
    16. })  
    17. public class DAreasServiceImplTest {  
    18.     @Resource  
    19.     private DAreasService dAreasService;  
    20.   
    21.     @Test  
    22.     public void testSelectSubDistricts0() throws Exception {  
    23.         List<DAreas> dAreases =  this.dAreasService.selectSubDistricts0();  
    24.         System.out.println(dAreases.size());  
    25.     }  
  1. 转载自:https://blog.csdn.net/xcwll_sina/article/details/49277645

猜你喜欢

转载自www.cnblogs.com/silentdoer/p/8904050.html