源码附件: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 -->