spring项目,服务或接口开发完成后,需要进行单元测试,测试通过后才可以提交QA进行功能验证测试。那么如何进行单元测试呢?

1.首先新建test目录

   spring项目单元测试

2.test目录下,新建java目录,并且设置为“Test sources Root”。java目录右键-选择Mark Directory as-选择Test Sources Root

   spring项目单元测试

3.java目录下新建包。比如com.xxx.xxx(包名随意)

   spring项目单元测试

4.包下面新建抽象测试基类AbstractTest。其他测试类都继承这个基类

package com.xxxx.xxxx;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:/config/appcontext-*.xml"})   //加载spring配置文件
public abstract class AbstractTest {
}

注:这一步需要引入依赖

<dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-test</artifactId>
     <version>${spring.version}</version>
 </dependency>

5.测试类继承AbstractTest   

package com.xxx.xxxx;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;


public class SyncTest extends AbstractTest {

    @Autowired
    SyncLongSqlInfoJob syncLongSqlInfoJob;

    @Test
    public void test2() throws Exception {
        syncLongSqlInfoJob.execute();
    }

}

   

 

相关文章:

  • 2022-01-18
  • 2022-01-23
  • 2022-12-23
  • 2021-10-06
  • 2022-12-23
  • 2021-11-04
猜你喜欢
  • 2022-12-23
  • 2022-01-04
  • 2021-06-09
  • 2021-09-03
  • 2021-05-13
  • 2022-12-23
  • 2021-09-04
相关资源
相似解决方案