从零开始的springboot生活(二)--写一下测试吧

上次写完了一个controller,这次我可以讲讲怎么写一下测试,单元测试对于我们开发人员来说是一个十分重要的知识点,所以我特地记录一下

增加单元测试需要在pom.xml加上这个,有了就不用加了

'''

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

'''


先介绍一下,测试的基本用法分为

Service层单元测试

Controller层单元测试

新断言assertThat使用

单元测试的回滚

在写测试类之前,先新建service类,


在建立了service类之后,再新建test类,其中我继承了DemoTestApplicationTests是因为我懒得写@RunWith(SpringRunner.class)

和@SpringBootTest,所以通过继承的方式来实现


代码为:

'''

package com.example.test.demotest.test;

import com.example.test.demotest.DemoTestApplicationTests;

import com.example.test.demotest.service.IGetService;

import org.junit.Assert;

import org.junit.Test;

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

import static org.assertj.core.internal.bytebuddy.matcher.ElementMatchers.is;

/**

* @author qiubo

* @date 2019/1/20

*/

public class ServiceTestextends DemoTestApplicationTests {

@Autowired

  IGetServicegetService;

@Test

    public void test(){

Assert.assertEquals(getService.getTest(),"testService");

//Assert.assertEquals(getService.getTest(),"test");

    }

}

'''

运行这个类,你会发现没有问题,当你取消掉Assert.assertEquals(getService.getTest(),"test");这句注释,你会发现程序报错,这就说明他与你期望的值并不一样

controller的测试:

'''

package com.example.test.demotest.test;

import com.example.test.demotest.DemoTestApplicationTests;

import com.example.test.demotest.controller.TestController;

import com.example.test.demotest.service.IGetService;

import org.junit.Assert;

import org.junit.Before;

import org.junit.Test;

import org.mockito.InjectMocks;

import org.mockito.MockitoAnnotations;

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

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 org.springframework.test.web.servlet.setup.MockMvcBuilders;

/**

* @author qiubo

* @date 2019/1/20

*/

public class ServiceTestextends DemoTestApplicationTests {

@InjectMocks

    private TestControllercontroller;

@Autowired

    IGetServicegetService;

private MockMvcmockMvc;

@Before

    public void setUp()throws Exception {

this.mockMvc = MockMvcBuilders.standaloneSetup(controller).build();

MockitoAnnotations.initMocks(this);

}

@Test

    public void test(){

Assert.assertEquals(getService.getTest(),"testService");

//Assert.assertEquals(getService.getTest(),"test");

    }

@Test

    public void testController()throws Exception {

MvcResult mvcResult =this.mockMvc.perform(MockMvcRequestBuilders.get("/test"))

.andExpect(MockMvcResultMatchers.status().isOk())

.andExpect(MockMvcResultMatchers.content().string("test"))

.andReturn();

System.out.println(mvcResult.getResponse().getContentAsString());

}

}

'''

展示有test

剩下的回滚和断言可以看这个网址,它上面介绍的非常好

http://tengj.top/2017/12/28/springboot12/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容