1. Add Junit4 and SpringJUnit4 support
- <!-- junit -->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <scope>test</scope>
- </dependency>
- <!-- spring-test -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- </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:
- package net.wll.web.service.impl;
- import org.junit.Test;
- import static org.junit.Assert.*;
- public class DAreasServiceImplTest {
- @Test
- public void testSelectSubDistricts() throws Exception {
- }
- @Test
- public void testSelectSubDistricts0() throws Exception {
- }
- }
4. Add Spring-test support
- package net.xuele.activity.service.impl;
- import net.xuele.activity.domain.DAreas;
- import net.xuele.activity.service.DAreasService;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
- import javax.annotation.Resource;
- import java.util.List;
- @RunWith(SpringJUnit4ClassRunner.class)
- /** Inject related configuration files: multiple configuration files can be written **/
- @ContextConfiguration(locations={"classpath:META-INF/spring/application-services.xml",
- "classpath:META-INF/spring/applicationContext-persist.xml"
- })
- public class DAreasServiceImplTest {
- @Resource
- private DAreasService dAreasService;
- @Test
- public void testSelectSubDistricts0() throws Exception {
- List<DAreas> dAreases = this.dAreasService.selectSubDistricts0();
- System.out.println(dAreases.size());
- }
- }
- Reprinted from: https://blog.csdn.net/xcwll_sina/article/details/49277645