单元测试(unit testing),是指对软件中的最小可测试单元进行检查和验证。对于单元测试中单元的含义,通常来讲,要根据实际状况去断定其具体含义,如C语言中单元指一个函数,Java里单元指一个类,图形化的软件中能够指一个窗口或一个菜单等。总的来讲,单元就是人为规定的最小的被测功能模块。单元测试是在软件开发过程当中要进行的最低级别的测试活动,软件的独立单元将在与程序的其余部分相隔离的状况下进行测试。java
参考网址: http://softwaretestingfundamentals.com/unit-testing/git
关键词: individual, smallestgithub
单元测试的目的是用来确保程式的逻辑如你预期的方式执行,而并非用来验证是否符合客户的需求的!经过单元测试来创建一道坚实的保障,确保代码在往后的修改中不会被破坏掉。markdown
是否是很失望?单元测试并非用来验证代码是否符合需求的。架构
参考阿里巴巴开发手册(详尽版)app
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.
@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("test@qq.com"), "t***@qq.com"), }
<dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>2.4.15</version> <scope>test</scope> </dependency> <dependency> <groupId>org.spockframework</groupId> <artifactId>spock-core</artifactId> <version>1.0-groovy-2.4</version> <scope>test</scope> </dependency>
<build> <finalName>${project.name}</finalName> <plugins> <plugin> <groupId>org.codehaus.gmavenplus</groupId> <artifactId>gmavenplus-plugin</artifactId> <executions> <execution> <goals> <goal>compile</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.20.1</version> <configuration> <useFile>false</useFile> <includes> <include>**/*Test.java</include> <include>**/*Test.groovy</include> </includes> </configuration> </plugin> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> </plugin> </plugins> </build>
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<List<GitlabProjectDTO>>(new ArrayList<GitlabProjectDTO>(), HttpStatus.ACCEPTED) when: def result = gitlabProjectRepo.getGitlabProjectByGitUrl(str+".git") then: 1 * gitlabProjectClient.findProjects(_,_,_,_,_) >> response result == Optional.EMPTY } }
groovy的语法, 后面的文章咱们会介绍.maven
若是你以为我写的不错, 就点个赞吧,点赞是一种态度.若是你想跟我多交流, 或者给我投稿, 也很是支持, 但只支持原创啊: 本人公众号stormlingide
听说你们都在点"在看", 动动你的小手点一下吧! 感谢!函数