美文网首页从零开始的springboot生活
从零开始的springboot生活(二)--写一下测试吧

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

作者: 邱风扫落叶 | 来源:发表于2019-01-20 23:49 被阅读0次

上次写完了一个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/

相关文章

网友评论

    本文标题:从零开始的springboot生活(二)--写一下测试吧

    本文链接:https://www.haomeiwen.com/subject/pzjfjqtx.html