idea intellij to unit test Spring

1. Add Junit4 and SpringJUnit4 support

  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. Create a test class

(wins: right-click menu -------> Go to (G) -------> Test (E) to quickly create a corresponding test class in the test package and the same directory)

(ios: IntelliJ IDEA provides a shortcut Cmd + Shift + T as a navigation between classes and tests. Also allows users to create a test class there.)

 

3. The generated code is as follows:

  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. Add Spring-test support

    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. /** Inject related configuration files: multiple configuration files can be written **/  
    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. Reprinted from: https://blog.csdn.net/xcwll_sina/article/details/49277645

     

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324605494&siteId=291194637