1. 什么是单元测试
单元测试(unit testing),是指对软件中的最小可测试单元进行检查和验证。对于单元测试中单元的含义,一般来说,要根据实际情况去判定其具体含义,如C语言中单元指一个函数,Java里单元指一个类,图形化的软件中可以指一个窗口或一个菜单等。总的来说,单元就是人为规定的最小的被测功能模块。单元测试是在软件开发过程中要进行的最低级别的测试活动,软件的独立单元将在与程序的其他部分相隔离的情况下进行测试。
参考网址: http://softwaretestingfundamentals.com/unit-testing/
关键词: individual, smallest
2. 单元测试的目的
单元测试的目的是用来确保程式的逻辑如你预期的方式执行,而并不是用来验证是否符合客户的需求的!通过单元测试来建立一道坚实的保障,确保代码在日后的修改中不会被破坏掉。
是不是很失望?单元测试并不是用来验证代码是否符合需求的。
3. 单元测试的原则
参考阿里巴巴开发手册(详尽版)
4. 常用的测试架构:
- JUnit https://junit.org/
- TestNG https://testng.org/
- Hamcrest http://hamcrest.org/
- Mockito https://site.mockito.org/
- PowerMock https://github.com/powermock/powermock
- EasyMock http://easymock.org/
- Spock http://spockframework.org/
5. JUnit
5.1 JUnit annotations
- @Before
- @After
- @BeforeClass
- @AfterClass
- @Ignore
- @RunWith
- @Test
5.2 JUnit asset method
S.No. Method Description
1. void assertEquals(boolean expected, boolean actual) It checks whether two values are equals similar to equals method of Object class
2. void assertFalse(boolean condition) functionality is to check that a condition is false.
3. void assertNotNull(Object object) "assertNotNull" functionality is to check that an object is not null.
4. void assertNull(Object object) "assertNull" functionality is to check that an object is null.
5. void assertTrue(boolean condition) "assertTrue" functionality is to check that a condition is true.
6. void fail() If you want to throw any assertion error, you have fail() that always results in a fail verdict.
7. void assertSame([String message] "assertSame" functionality is to check that the two objects refer to the same object.
8. void assertNotSame([String message] "assertNotSame" functionality is to check that the two objects do not refer to the same object.
5.3 JUint编写单元测试
@Test(description="mask chinese name")
public void maskChineseName() {
assertEquals(DesensitizedUtils.maskChinesName(null), null),
assertEquals(DesensitizedUtils.maskChinesName("张三"), "张*"),
assertEquals(DesensitizedUtils.maskChinesName("赵钱李"), "赵*李"),
}
@Test(descriptioin="mask email")
public void maskEmail() {
assertEquals(DesensitizedUtils.maskEmail(null), null),
assertEquals(DesensitizedUtils.maskEmail("[email protected]"), "t***@qq.com"),
}
6. Groovy Spock使用介绍(推介)
6.1 maven配置
org.codehaus.groovy
groovy-all
2.4.15
test
org.spockframework
spock-core
1.0-groovy-2.4
test
6.2 相关的插件
${project.name}
org.codehaus.gmavenplus
gmavenplus-plugin
compile
maven-surefire-plugin
2.20.1
false
**/*Test.java
**/*Test.groovy
org.jacoco
jacoco-maven-plugin
6.3 Groovy单侧结构示例
6.4 spock单侧代码示例
import spock.lang.Specification
class GitlabProjectRepoImplTest extends Specification{
// 申明变量
GitlabProjectRepo gitlabProjectRepo
// 通过Mock初始化对象, 类似@Resource或者@Autowired创建的实例, 不过是虚拟的
def gitlabProjectClient = Mock(GitlabProjectClient)
// 类似于我们JUnit的@Before
def setup(){
gitlabProjectRepo = new GitlabProjectRepoImpl(gitlabProjectClient:gitlabProjectClient,githomeProjectClient:githomeProjectClient,ciGitlabProperties:ciGitlabProperties,ciGithomeProperties:ciGithomeProperties,pipelineMapper:pipelineMapper)
}
// 具体的语法后面会介绍
def "get GitlabProject By GitUrl success" (){
given:
def gitlabProjectDTO = new GitlabProjectDTO()
gitlabProjectDTO.setName("test")
def str = "http://www.baidu.com/git/xx"
def response = new ResponseEntity>(new ArrayList(), HttpStatus.ACCEPTED)
when:
def result = gitlabProjectRepo.getGitlabProjectByGitUrl(str+".git")
then:
1 * gitlabProjectClient.findProjects(_,_,_,_,_) >> response
result == Optional.EMPTY
}
}
groovy的语法, 后面的文章我们会介绍.
如果你觉得我写的不错, 就点个赞吧,点赞是一种态度.如果你想跟我多交流, 或者给我投稿, 也非常支持, 但只支持原创啊: 本人公众号stormling
据说大家都在点"在看", 动动你的小手点一下吧! 感谢!