欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot之单元测试

程序员文章站 2022-04-14 20:45:34
springboot在写完之后,肯定都需要进行单元测试,如下给出一些样例 工程层次结构如图 代码如下: controller: service: 启动程序: 测试类: ......

springboot在写完之后,肯定都需要进行单元测试,如下给出一些样例

工程层次结构如图

springboot之单元测试

代码如下:

controller:

package com.rookie.bigdata.controller;

import com.rookie.bigdata.domain.user;
import com.rookie.bigdata.service.userservice;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.restcontroller;


/**
 * controller层
 * created by on 2018/9/28.
 */
@restcontroller
public class usercontroller {

    @autowired
    private userservice userservice;

    /**
     * 查询用户
     *
     * @return
     */
    @getmapping(value = "/user")
    public user finduser() {
        return userservice.findone(10);
    }

}
user:
package com.rookie.bigdata.domain;

/**
 * domain实体对象
 * created by on 2018/9/28.
 */
public class user {
    private int id;
    private string name;
    private integer age;

    public integer getage() {
        return age;
    }

    public void setage(integer age) {
        this.age = age;
    }

    public user() {
    }

    public int getid() {
        return id;
    }

    public void setid(int id) {
        this.id = id;
    }

    public string getname() {
        return name;
    }

    public void setname(string name) {
        this.name = name;
    }

    @override
    public string tostring() {
        return "user{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

 

service:

package com.rookie.bigdata.service;

import com.rookie.bigdata.domain.user;
import org.springframework.stereotype.service;

/**
 * service层
 * created by  on 2018/9/28.
 */
@service
public class userservice {

    public user findone(integer id) {
        user user = new user();
        user.setid(id);
        user.setname("张三");
        user.setage(23);
        return user;
    }
}

 

启动程序:

package com.rookie.bigdata;

import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.scheduling.annotation.enablescheduling;

/**
 * 应用程序启动类
 * created by on 2018/8/2.
 */
@springbootapplication
public class application {
    public static void main(string[] args) {
        springapplication.run(application.class, args);

    }
}

 

测试类:

package com.rookie.bigdata.controller;

import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.autoconfiguremockmvc;
import org.springframework.boot.test.context.springboottest;
import org.springframework.http.mediatype;
import org.springframework.test.context.junit4.springrunner;
import org.springframework.test.web.servlet.mockmvc;
import org.springframework.test.web.servlet.mvcresult;
import org.springframework.test.web.servlet.request.mockmvcrequestbuilders;
import org.springframework.test.web.servlet.result.mockmvcresultmatchers;

import static org.junit.assert.*;

/**
 * 测试contoller层
 * created by on 2018/9/28.
 */
@runwith(springrunner.class)
@springboottest
@autoconfiguremockmvc
public class usercontrollertest {

    @autowired
    private mockmvc mvc;

    @test
    public void finduser() throws exception {
//        mvc.perform(mockmvcrequestbuilders.get("/user"))
//                .andexpect(mockmvcresultmatchers.status().isok());

        mvcresult mvcresult = mvc.perform(mockmvcrequestbuilders.get("/user"))
                .andexpect(mockmvcresultmatchers.status().isok())//模拟发送get请求
                .andexpect(mockmvcresultmatchers.content().contenttype(mediatype.application_json_utf8))//预期返回值的媒体类型 application/json;charset=utf-8
                .andreturn();//返回执行的请求结果

        system.out.println(mvcresult.getresponse().getcontentasstring());


    }

}

 

 

package com.rookie.bigdata.service;

import com.rookie.bigdata.domain.user;
import org.junit.assert;
import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.test.context.springboottest;
import org.springframework.test.context.junit4.springrunner;


/**
 * 测试service层
 * created by on 2018/9/28.
 */
@runwith(springrunner.class)
@springboottest
public class userservicetest {

    @autowired
    private userservice userservice;

    @test
    public void findone() throws exception {

        user user = userservice.findone(1);

        assert.assertequals(new integer(23),user.getage());
    }

}