Spring的测试

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring的测试相关的知识,希望对你有一定的参考价值。

spring测试要引用junit及spring-test

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

代码:

配置文件的引入:

单个文件:@ContextConfiguration(locations ="classpath:spring-mybatis.xml")

多个文件:@ContextConfiguration(locations ={"classpath:spring-mybatis.xml","classpath:application-context-provider.xml"})

import com.zoe.aop.dto.*;
import com.zoe.aop.service.out.DeptService;
import org.junit.Assert;
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;

/**
 * Created by gyoung on 2016/1/23.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations ={"classpath:spring-mybatis.xml","classpath:application-context-provider.xml"})
public class DeptServerTest {

    @Autowired
    private DeptService deptService;

    @Test
    public void deptSelectTest() {
        DeptDto model= deptService.getOneById("2");
        Assert.assertTrue(model.getId().equals("2"));
    }

    @Test
    public void updateTest(){
        DeptDto model= new DeptDto();
        model.setId("2");
        model.setName("111");
        deptService.update(model);
        String id=model.getId();
    }

    @Test
    public void pageTest(){
        QueryPage page=new QueryPage(1,5);
        ServiceResultT<CorePageList> resultT= deptService.getList(page);
    }

    @Test
    public void deleteTtest(){
        ServiceResult result= deptService.deleteById("1203");
    }

    @Test
    public void updateDbTest(){

    }
}

 

以上是关于Spring的测试的主要内容,如果未能解决你的问题,请参考以下文章

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段

What's the difference between @Component, @Repository & @Service annotations in Spring?(代码片段

CTS测试CtsWindowManagerDeviceTestCases模块的testShowWhenLockedImeActivityAndShowSoftInput测试fail项解决方法(代码片段

;~ 小部分AutoHotkey源代码片段测试模板2019年10月9日.ahk