or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

advanced.mdbasic-assertions.mdcollections.mdconditions.mddates-times.mdexceptions.mdindex.mdsoft-assertions.mdstrings.md

basic-assertions.mddocs/

0

# Basic Assertions

1

2

Basic assertions cover primitive types, objects, and fundamental assertion patterns that form the foundation of AssertJ testing.

3

4

## Core Imports

5

6

```java

7

import static org.assertj.core.api.Assertions.*;

8

```

9

10

## Capabilities

11

12

### Boolean Assertions

13

14

Assertions for boolean values and conditions.

15

16

```java { .api }

17

BooleanAssert assertThat(boolean actual)

18

BooleanAssert assertThat(Boolean actual)

19

20

// Boolean-specific methods

21

BooleanAssert isTrue()

22

BooleanAssert isFalse()

23

BooleanAssert isEqualTo(boolean expected)

24

BooleanAssert isNotEqualTo(boolean expected)

25

```

26

27

Usage examples:

28

```java

29

assertThat(true).isTrue();

30

assertThat(false).isFalse();

31

assertThat(user.isActive()).isTrue();

32

assertThat(validationResult.hasErrors()).isFalse();

33

```

34

35

### Integer Assertions

36

37

Assertions for integer values with numeric comparisons.

38

39

```java { .api }

40

IntegerAssert assertThat(int actual)

41

IntegerAssert assertThat(Integer actual)

42

43

// Integer-specific methods

44

IntegerAssert isPositive()

45

IntegerAssert isNegative()

46

IntegerAssert isZero()

47

IntegerAssert isNotZero()

48

IntegerAssert isEven()

49

IntegerAssert isOdd()

50

IntegerAssert isGreaterThan(int expected)

51

IntegerAssert isGreaterThanOrEqualTo(int expected)

52

IntegerAssert isLessThan(int expected)

53

IntegerAssert isLessThanOrEqualTo(int expected)

54

IntegerAssert isBetween(int start, int end)

55

IntegerAssert isStrictlyBetween(int start, int end)

56

IntegerAssert isCloseTo(int expected, Offset<Integer> offset)

57

```

58

59

Usage examples:

60

```java

61

assertThat(42).isPositive().isEven();

62

assertThat(-5).isNegative().isOdd();

63

assertThat(score).isBetween(0, 100);

64

assertThat(actualValue).isCloseTo(expectedValue, offset(5));

65

```

66

67

### Long Assertions

68

69

Assertions for long values with numeric comparisons.

70

71

```java { .api }

72

LongAssert assertThat(long actual)

73

LongAssert assertThat(Long actual)

74

75

// Long-specific methods (same as Integer)

76

LongAssert isPositive()

77

LongAssert isNegative()

78

LongAssert isZero()

79

LongAssert isNotZero()

80

LongAssert isEven()

81

LongAssert isOdd()

82

LongAssert isGreaterThan(long expected)

83

LongAssert isGreaterThanOrEqualTo(long expected)

84

LongAssert isLessThan(long expected)

85

LongAssert isLessThanOrEqualTo(long expected)

86

LongAssert isBetween(long start, long end)

87

LongAssert isStrictlyBetween(long start, long end)

88

LongAssert isCloseTo(long expected, Offset<Long> offset)

89

```

90

91

### Double and Float Assertions

92

93

Assertions for floating-point numbers with precision handling.

94

95

```java { .api }

96

DoubleAssert assertThat(double actual)

97

DoubleAssert assertThat(Double actual)

98

FloatAssert assertThat(float actual)

99

FloatAssert assertThat(Float actual)

100

101

// Double/Float-specific methods

102

DoubleAssert isNaN()

103

DoubleAssert isNotNaN()

104

DoubleAssert isInfinite()

105

DoubleAssert isFinite()

106

DoubleAssert isPositive()

107

DoubleAssert isNegative()

108

DoubleAssert isZero()

109

DoubleAssert isNotZero()

110

DoubleAssert isCloseTo(double expected, Offset<Double> offset)

111

DoubleAssert isCloseTo(double expected, Percentage percentage)

112

DoubleAssert isBetween(double start, double end)

113

DoubleAssert isStrictlyBetween(double start, double end)

114

```

115

116

Usage examples:

117

```java

118

assertThat(3.14159).isCloseTo(3.14, offset(0.01));

119

assertThat(percentage).isCloseTo(50.0, withinPercentage(5));

120

assertThat(Double.NaN).isNaN();

121

assertThat(1.0 / 0.0).isInfinite();

122

```

123

124

### Character Assertions

125

126

Assertions for character values and Unicode properties.

127

128

```java { .api }

129

CharacterAssert assertThat(char actual)

130

CharacterAssert assertThat(Character actual)

131

132

// Character-specific methods

133

CharacterAssert isEqualTo(char expected)

134

CharacterAssert isNotEqualTo(char expected)

135

CharacterAssert isGreaterThan(char expected)

136

CharacterAssert isLessThan(char expected)

137

CharacterAssert isUpperCase()

138

CharacterAssert isLowerCase()

139

CharacterAssert isDigit()

140

CharacterAssert isLetter()

141

CharacterAssert isLetterOrDigit()

142

CharacterAssert isWhitespace()

143

```

144

145

Usage examples:

146

```java

147

assertThat('A').isUpperCase().isLetter();

148

assertThat('5').isDigit();

149

assertThat(' ').isWhitespace();

150

assertThat(firstChar).isGreaterThan('a').isLowerCase();

151

```

152

153

### Byte and Short Assertions

154

155

Assertions for byte and short numeric types.

156

157

```java { .api }

158

ByteAssert assertThat(byte actual)

159

ByteAssert assertThat(Byte actual)

160

ShortAssert assertThat(short actual)

161

ShortAssert assertThat(Short actual)

162

163

// Byte/Short-specific methods (similar to Integer)

164

ByteAssert isPositive()

165

ByteAssert isNegative()

166

ByteAssert isZero()

167

ByteAssert isNotZero()

168

ByteAssert isGreaterThan(byte expected)

169

ByteAssert isLessThan(byte expected)

170

ByteAssert isBetween(byte start, byte end)

171

```

172

173

### Object Assertions

174

175

Generic assertions for any object type.

176

177

```java { .api }

178

ObjectAssert<T> assertThat(T actual)

179

180

// Object methods

181

ObjectAssert<T> isEqualTo(Object expected)

182

ObjectAssert<T> isNotEqualTo(Object expected)

183

ObjectAssert<T> isNull()

184

ObjectAssert<T> isNotNull()

185

ObjectAssert<T> isSameAs(Object expected)

186

ObjectAssert<T> isNotSameAs(Object expected)

187

ObjectAssert<T> isInstanceOf(Class<?> type)

188

ObjectAssert<T> isNotInstanceOf(Class<?> type)

189

ObjectAssert<T> isInstanceOfAny(Class<?>... types)

190

ObjectAssert<T> isExactlyInstanceOf(Class<?> type)

191

ObjectAssert<T> isIn(Object... values)

192

ObjectAssert<T> isIn(Iterable<?> values)

193

ObjectAssert<T> isNotIn(Object... values)

194

ObjectAssert<T> isNotIn(Iterable<?> values)

195

ObjectAssert<T> hasToString(String expected)

196

ObjectAssert<T> hasHashCode(int expected)

197

```

198

199

Usage examples:

200

```java

201

assertThat(user).isNotNull().isInstanceOf(User.class);

202

assertThat(result).isSameAs(expectedInstance);

203

assertThat(status).isIn("ACTIVE", "PENDING", "INACTIVE");

204

assertThat(person).hasToString("Person{name='John', age=30}");

205

```

206

207

### Class Assertions

208

209

Assertions for Class objects and type information.

210

211

```java { .api }

212

ClassAssert assertThat(Class<?> actual)

213

214

// Class-specific methods

215

ClassAssert isAssignableFrom(Class<?> other)

216

ClassAssert isNotAssignableFrom(Class<?> other)

217

ClassAssert isAnnotation()

218

ClassAssert isNotAnnotation()

219

ClassAssert isInterface()

220

ClassAssert isNotInterface()

221

ClassAssert isFinal()

222

ClassAssert isNotFinal()

223

ClassAssert isAbstract()

224

ClassAssert isNotAbstract()

225

ClassAssert hasAnnotation(Class<? extends Annotation> annotation)

226

ClassAssert hasAnnotations(Class<? extends Annotation>... annotations)

227

```

228

229

Usage examples:

230

```java

231

assertThat(ArrayList.class).isAssignableFrom(List.class);

232

assertThat(MyInterface.class).isInterface();

233

assertThat(MyEntity.class).hasAnnotation(Entity.class);

234

```

235

236

### BigDecimal and BigInteger Assertions

237

238

Assertions for high-precision numeric types.

239

240

```java { .api }

241

BigDecimalAssert assertThat(BigDecimal actual)

242

BigIntegerAssert assertThat(BigInteger actual)

243

244

// BigDecimal methods

245

BigDecimalAssert isCloseTo(BigDecimal expected, Offset<BigDecimal> offset)

246

BigDecimalAssert isCloseTo(BigDecimal expected, Percentage percentage)

247

BigDecimalAssert isZero()

248

BigDecimalAssert isNotZero()

249

BigDecimalAssert isPositive()

250

BigDecimalAssert isNegative()

251

BigDecimalAssert isBetween(BigDecimal start, BigDecimal end)

252

253

// BigInteger methods

254

BigIntegerAssert isPositive()

255

BigIntegerAssert isNegative()

256

BigIntegerAssert isZero()

257

BigIntegerAssert isNotZero()

258

BigIntegerAssert isEven()

259

BigIntegerAssert isOdd()

260

```

261

262

Usage examples:

263

```java

264

assertThat(new BigDecimal("3.14159"))

265

.isCloseTo(new BigDecimal("3.14"), offset(new BigDecimal("0.01")));

266

267

assertThat(BigInteger.valueOf(1000000)).isPositive().isEven();

268

```

269

270

### Common Assertion Patterns

271

272

All assertion types support these common methods:

273

274

```java { .api }

275

// Description and context

276

AbstractAssert<SELF, ACTUAL> as(String description)

277

AbstractAssert<SELF, ACTUAL> as(String description, Object... args)

278

AbstractAssert<SELF, ACTUAL> describedAs(String description)

279

AbstractAssert<SELF, ACTUAL> describedAs(String description, Object... args)

280

281

// Custom validation

282

AbstractAssert<SELF, ACTUAL> satisfies(Consumer<ACTUAL> requirements)

283

AbstractAssert<SELF, ACTUAL> satisfiesAnyOf(Consumer<ACTUAL>... requirements)

284

285

// Failure control

286

AbstractAssert<SELF, ACTUAL> overridingErrorMessage(String newErrorMessage)

287

AbstractAssert<SELF, ACTUAL> withFailMessage(String failureMessage)

288

```

289

290

Usage examples:

291

```java

292

assertThat(score)

293

.as("User's final exam score")

294

.isBetween(0, 100);

295

296

assertThat(user)

297

.satisfies(u -> {

298

assertThat(u.getName()).isNotBlank();

299

assertThat(u.getAge()).isGreaterThan(0);

300

});

301

```

302

303

## Types

304

305

```java { .api }

306

// Numeric offset for comparisons

307

class Offset<T extends Number> {

308

static Offset<Double> offset(Double value)

309

static Offset<Float> offset(Float value)

310

static Offset<Integer> offset(Integer value)

311

static Offset<Long> offset(Long value)

312

static Offset<BigDecimal> offset(BigDecimal value)

313

}

314

315

// Percentage for comparisons

316

class Percentage {

317

static Percentage withPercentage(Double percentage)

318

static Percentage withPercentage(Integer percentage)

319

}

320

321

// Consumer interface for custom validation

322

interface Consumer<T> {

323

void accept(T t);

324

}

325

```