Spring的单元自动化测试--业务逻辑层&控制器层单元自动化测试

 

源码附件:https://gitee.com/steffens/httpinterface/tree/master/bookcode38382

 

业务逻辑层单元自动化测试

package com.lujiatao.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.lujiatao.springboot.Application;

@SpringBootTest(classes = { Application.class })
public class CalculatorForPpiControllerTest extends AbstractTestNGSpringContextTests {
	@Autowired
	private CalculatorForPpiController controller;
	private MockMvc mock;
	private RequestBuilder request;
	private String width;
	private String height;
	private String size;

	@BeforeClass
	public void init() {
		mock = MockMvcBuilders.standaloneSetup(controller).build();
		width = "750";
		height = "1334";
		size = "4.7";
	}

	@Test
	public void testCase1() {
		sendRequest(width, height, size, "326");
	}

	@Test
	public void testCase2() {
		sendRequest("-1", height, size, "-1");
	}

	@Test
	public void testCase3() {
		sendRequest("0", height, size, "-1");
	}

	@Test
	public void testCase4() {
		sendRequest(width, "-1", size, "-1");
	}

	@Test
	public void testCase5() {
		sendRequest(width, "0", size, "-1");
	}

	@Test
	public void testCase6() {
		sendRequest(width, height, "-1", "-1");
	}

	@Test
	public void testCase7() {
		sendRequest(width, height, "0", "-1");
	}

	private void sendRequest(String width, String height, String size, String expected) {
		request = MockMvcRequestBuilders.post("/calculate").param("width", width).param("height", height).param("size",
				size);
		try {
			mock.perform(request).andExpect(MockMvcResultMatchers.jsonPath("PPI").value(expected));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

 

 

控制器层单元自动化测试

package com.lujiatao.springboot.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.lujiatao.springboot.Application;

@SpringBootTest(classes = { Application.class })
public class CalculatorForPpiServiceTest extends AbstractTestNGSpringContextTests {
	@Autowired
	private CalculatorForPpiService service;
	private int height;
	private int width;
	private double size;

	@BeforeClass
	public void init() {
		height = 750;
		width = 1334;
		size = 4.7;
	}

	@Test
	public void testCase1() {
		Assert.assertEquals(326, service.calculate(width, height, size));
	}

	@Test
	public void testCase2() {
		Assert.assertEquals(-1, service.calculate(-1, height, size));
	}

	@Test
	public void testCase3() {
		Assert.assertEquals(-1, service.calculate(0, height, size));
	}

	@Test
	public void testCase4() {
		Assert.assertEquals(-1, service.calculate(width, -1, size));
	}

	@Test
	public void testCase5() {
		Assert.assertEquals(-1, service.calculate(width, 0, size));
	}

	@Test
	public void testCase6() {
		Assert.assertEquals(-1, service.calculate(width, height, -1));
	}

	@Test
	public void testCase7() {
		Assert.assertEquals(-1, service.calculate(width, height, 0));
	}

}

 

 

业务逻辑层单元自动化测试-TestNG.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
	<test thread-count="5" name="Test">
		<classes>
			<class
				name="com.lujiatao.springboot.service.CalculatorForPpiServiceTest" />
		</classes>
	</test> <!-- Test -->
</suite> <!-- Suite -->

 

 

 

 控制器层单元自动化测试-TestNG.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
	<test thread-count="5" name="Test">
		<classes>
			<class
				name="com.lujiatao.springboot.controller.CalculatorForPpiControllerTest" />
		</classes>
	</test> <!-- Test -->
</suite> <!-- Suite -->

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值