单篇长文TestNG从入门到精通

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 简介 TestNG是Test Next Generation的缩写,它的灵感来自于JUnit和NUnit,在它们基础上增加了很多很牛的功能,比如说: 注解。 多线程,比如所有方法都在各自线程中,一个测试类一个线程等。 验证代码是否多线程安全。 灵活的测试配置。 支持数据驱动(@DataProvider)。 支持参数化。 强大的执行机制(不需要TestSuite)。 能跟各种工具结合(比如IDEA、Maven等)。 内嵌BeanShell。 提供运行时和日志的JDK函数(不需要添加dependencies)。 提供应用服务器测试依赖的方法。 一句话总结就是,TestNG强大到能覆盖所有测试类型:单元测试、功能测试、端到端测试、集成测试等等等。 一个简单的示例如下: package example1; import org.testng.annotations.*; public class SimpleTest { @BeforeClass public void setUp() { // code that will be invoked when this test is instantiated } @Test(groups = { "fast" }) public void aFastTest() { System.out.println("Fast test"); } @Test(groups = { "slow" }) public void aSlowTest() { System.out.println("Slow test"); } } setUp()会在测试类创建后,测试方法执行前运行。 测试方法名字可以任意的,TestNG通过@Test注解来标识。 可以把测试方法按group分组。 然后使用xml来配置运行策略: ...

Allure+Maven+JUnit5配置实践

为什么要用JUnit5?

2026-01-20 · 1 分钟 · 61 字 · 测试开发刚哥 · Java生态 · #Java

fastjson知多少

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 fastjson介绍 fastjson是一个Java库,用来在Java对象和JSON之间相互转换。 fastjson的目标(官方原话): 我翻译一下: 性能非常好; 封装非常高; 转换非常强; 泛型也支持; 自定义表示; 复杂也支持; 这6个目标,书读少的我理解起来费劲,看着这些字,视线开始模糊…突然这些字动了,不断变幻,凝聚成了两个字:“牛逼”。阿里巴巴也确实做到了,fastjson(24.9k star)作为国产原创库,PK掉了jackson(7.6k star)和gson(21.3k star),在国内应用领域坐上了头把交椅。 安装fastsjon Maven引入: <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>VERSION_CODE</version> </dependency> 版本可以在 https://mvnrepository.com/artifact/com.alibaba/fastjson 查找: 虽然2.0.x版本出来了,但是使用得多的还是1.2.x版本。至少得1.2.48以上,因为这以下的版本之前出过轰动全国的安全漏洞。 入门示例 Group.java public class Group { private Long id; private String name; private List<User> users = new ArrayList<User>(); public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<User> getUsers() { return users; } public void setUsers(List<User> users) { this.users = users; } public void addUser(User user) { users.add(user); } } User.java ...

2026-01-19 · 2 分钟 · 261 字 · 测试开发刚哥 · Java生态 · #Java · #库

二刷Java官方教程

第二次阅读Java官方教程,有些不一样的理解。内容比较散乱,个人意识流笔记。

Java官方笔记14流

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 Processing Data in Memory The Stream API is probably the second most important feature added to Java SE 8, after the lambda expressions. In a nutshell, the Stream API is about providing an implementation of the well known map-filter-reduce algorithm to the JDK. map-filter-reduce: List<Sale> sales = ...; // this is the list of all the sales int amountSoldInMarch = 0; for (Sale sale: sales) { if (sale.getDate().getMonth() == Month.MARCH) { amountSoldInMarch += sale.getAmount(); } } System.out.println("Amount sold in March: " + amountSoldInMarch); map:通过get取值,将部分字段映射到新数据(select字段) ...

Java官方笔记13集合

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 Storing Data The Collections Framework is the most widely used API of the JDK. 集合不是数据类型,它是JDK的API,可以用来存储数据等,相当于数据结构。 the Collections Framework is a set of interfaces that models different way of storing data in different types of containers. Then the Framework provides at least one implementation for each interface. There are two main categories of interfaces in the Collections Framework: collections and maps.(所以我猜想Python也是基于这样的考虑,设计了List和Dict,并作为Python基本数据类型,实际编码中要用到的也主要是这两类) That makes two main categories, Collection and Map, a subcategory, Queue, and a side category, Iterator. ...

Java官方笔记12异常

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 Exception Definition: An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions. the checked exception 比如,java.io.FileNotFoundException the error 比如,java.io.IOError the runtime exception 比如,NullPointerException error和runtime exception又叫做unchecked exception Catching and Handling Exceptions try { code } catch and finally blocks . . . try { } catch (ExceptionType name) { } catch (ExceptionType name) { } 使用|catch多个: ...

Java持久层框架Mybatis入门

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 MyBatis是什么 MyBatis是Java的持久层框架,GitHub的star数高达15.8k,是Java技术栈中最热门的ORM框架之一。它支持自定义SQL、存储过程以及高级映射,可以通过XML或注解来配置和映射原始类型、接口和Java POJOs为数据库中的记录。 POJOs,Plain Old Java Objects,纯的传统意义的Java对象,最基本的Java Bean只有属性加上属性的get和set方法。 安装 Maven pom.xml: <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>x.x.x</version> </dependency> 版本查询:https://mvnrepository.com/artifact/org.mybatis/mybatis SqlSessionFactory SqlSessionFactory的实例是每个MyBatis应用的核心,通过SqlSessionFactoryBuilder创建,能基于XML配置,也能使用Configuration类。 基于XML配置: String resource = "org/mybatis/example/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 实际项目中不会手动设置XML文件路径,而是直接读取classpath去找XML。 使用Configuration类: DataSource dataSource = BlogDataSourceFactory.getBlogDataSource(); TransactionFactory transactionFactory = new JdbcTransactionFactory(); Environment environment = new Environment("development", transactionFactory, dataSource); Configuration configuration = new Configuration(environment); configuration.addMapper(BlogMapper.class); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); 由于Java注解的一些限制以及某些MyBatis映射的复杂性,MyBatis会优先选择XML配置。在上面的示例中,MyBatis会根据classpath和BlogMapper.class去找BlogMapper.xml。 ...

Java官方笔记11包

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 Packages Definition: A package is a grouping of related types providing access protection and name space management. Note that types refers to classes, interfaces, enumerations, and annotation types. Enumerations and annotation types are special kinds of classes and interfaces, respectively, so types are often referred to in this section simply as classes and interfaces. 创建 //in the Draggable.java file package graphics; public interface Draggable { . . . } //in the Graphic.java file package graphics; public abstract class Graphic { . . . } //in the Circle.java file package graphics; public class Circle extends Graphic implements Draggable { . . . } //in the Rectangle.java file package graphics; public class Rectangle extends Graphic implements Draggable { . . . } //in the Point.java file package graphics; public class Point extends Graphic implements Draggable { . . . } //in the Line.java file package graphics; public class Line extends Graphic implements Draggable { . . . } 文件顶部第一行,每个文件只能有1个package关键字。 ...

Java官方笔记10注解

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 注解 注解的作用: Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings. Compile-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth. Runtime processing — Some annotations are available to be examined at runtime. 注解格式,使用@: @Entity 带key-value: @Author( name = "Benjamin Franklin", date = "3/27/2003" ) class MyClass { ... } 只有1个key时可以省略key: ...