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

strings.mddocs/

0

# Strings

1

2

String and CharSequence assertions for content validation, format checking, pattern matching, and text analysis.

3

4

## Core Imports

5

6

```java

7

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

8

```

9

10

## Capabilities

11

12

### String Assertions

13

14

Comprehensive assertions for String objects.

15

16

```java { .api }

17

StringAssert assertThat(String actual)

18

19

// Content assertions

20

StringAssert isEmpty()

21

StringAssert isNotEmpty()

22

StringAssert isBlank()

23

StringAssert isNotBlank()

24

StringAssert isNullOrEmpty()

25

StringAssert isNotNull()

26

27

// Length assertions

28

StringAssert hasLength(int expected)

29

StringAssert hasSizeLessThan(int expected)

30

StringAssert hasSizeGreaterThan(int expected)

31

StringAssert hasSizeBetween(int lowerBoundary, int higherBoundary)

32

33

// Prefix and suffix

34

StringAssert startsWith(String prefix)

35

StringAssert startsWithIgnoringCase(String prefix)

36

StringAssert doesNotStartWith(String prefix)

37

StringAssert endsWith(String suffix)

38

StringAssert endsWithIgnoringCase(String suffix)

39

StringAssert doesNotEndWith(String suffix)

40

41

// Content matching

42

StringAssert contains(String... values)

43

StringAssert containsIgnoringCase(String sequence)

44

StringAssert containsOnlyOnce(String sequence)

45

StringAssert containsSequence(String... values)

46

StringAssert containsSubsequence(String... values)

47

StringAssert doesNotContain(String... values)

48

StringAssert doesNotContainAnyWhitespaces()

49

StringAssert containsOnlyWhitespaces()

50

51

// Pattern matching

52

StringAssert matches(String regex)

53

StringAssert matches(Pattern pattern)

54

StringAssert doesNotMatch(String regex)

55

StringAssert doesNotMatch(Pattern pattern)

56

StringAssert matchesPattern(String pattern)

57

StringAssert matchesPattern(Pattern pattern)

58

59

// Case assertions

60

StringAssert isUpperCase()

61

StringAssert isLowerCase()

62

StringAssert isMixedCase()

63

64

// Equality with options

65

StringAssert isEqualTo(String expected)

66

StringAssert isEqualToIgnoringCase(String expected)

67

StringAssert isEqualToIgnoringWhitespace(String expected)

68

StringAssert isEqualToIgnoringNewLines(String expected)

69

StringAssert isEqualToNormalizingNewlines(String expected)

70

StringAssert isNotEqualTo(String other)

71

StringAssert isNotEqualToIgnoringCase(String other)

72

```

73

74

Usage examples:

75

```java

76

String message = "Hello World!";

77

assertThat(message)

78

.isNotEmpty()

79

.hasLength(12)

80

.startsWith("Hello")

81

.endsWith("!")

82

.contains("World")

83

.matches("Hello.*!");

84

85

String email = "user@domain.com";

86

assertThat(email)

87

.matches("\\w+@\\w+\\.\\w+")

88

.contains("@")

89

.doesNotContain(" ");

90

91

String input = " ";

92

assertThat(input).isBlank().containsOnlyWhitespaces();

93

94

String code = "PUBLIC STATIC VOID";

95

assertThat(code).isUpperCase();

96

```

97

98

### CharSequence Assertions

99

100

Generic assertions for any CharSequence implementation.

101

102

```java { .api }

103

CharSequenceAssert assertThat(CharSequence actual)

104

105

// CharSequence methods (similar to String but more generic)

106

CharSequenceAssert isEmpty()

107

CharSequenceAssert isNotEmpty()

108

CharSequenceAssert isBlank()

109

CharSequenceAssert isNotBlank()

110

CharSequenceAssert hasLength(int expected)

111

CharSequenceAssert startsWith(CharSequence prefix)

112

CharSequenceAssert endsWith(CharSequence suffix)

113

CharSequenceAssert contains(CharSequence... values)

114

CharSequenceAssert doesNotContain(CharSequence... values)

115

CharSequenceAssert isEqualTo(CharSequence expected)

116

CharSequenceAssert isEqualToIgnoringCase(CharSequence expected)

117

```

118

119

Usage examples:

120

```java

121

StringBuilder builder = new StringBuilder("Hello");

122

assertThat(builder)

123

.hasLength(5)

124

.startsWith("Hel")

125

.isNotEmpty();

126

127

StringBuffer buffer = new StringBuffer("Test Data");

128

assertThat(buffer)

129

.contains("Data")

130

.endsWith("Data");

131

```

132

133

### Character Array Assertions

134

135

Assertions for character arrays.

136

137

```java { .api }

138

CharArrayAssert assertThat(char[] actual)

139

140

// Character array methods

141

CharArrayAssert hasSize(int expected)

142

CharArrayAssert isEmpty()

143

CharArrayAssert isNotEmpty()

144

CharArrayAssert contains(char... values)

145

CharArrayAssert containsOnly(char... values)

146

CharArrayAssert containsSequence(char... sequence)

147

CharArrayAssert doesNotContain(char... values)

148

CharArrayAssert startsWith(char... sequence)

149

CharArrayAssert endsWith(char... sequence)

150

```

151

152

Usage examples:

153

```java

154

char[] chars = {'h', 'e', 'l', 'l', 'o'};

155

assertThat(chars)

156

.hasSize(5)

157

.contains('e', 'l')

158

.startsWith('h')

159

.endsWith('o');

160

```

161

162

### Advanced String Matching

163

164

Pattern-based and complex string validations.

165

166

```java { .api }

167

// Regular expression matching

168

StringAssert containsPattern(String regex)

169

StringAssert containsPattern(Pattern pattern)

170

StringAssert doesNotContainPattern(String regex)

171

StringAssert doesNotContainPattern(Pattern pattern)

172

173

// Line-based assertions

174

StringAssert hasLineCount(int expected)

175

StringAssert hasLinesCount(int expected)

176

177

// Normalization and comparison

178

StringAssert isEqualToNormalizingPunctuationAndWhitespace(String expected)

179

StringAssert isEqualToIgnoringNewLines(String expected)

180

StringAssert isSubstringOf(String sequence)

181

StringAssert isNotSubstringOf(String sequence)

182

183

// Encoding assertions

184

StringAssert asBase64Decoded()

185

StringAssert asHexString()

186

```

187

188

Usage examples:

189

```java

190

String multiLine = "Line 1\nLine 2\nLine 3";

191

assertThat(multiLine)

192

.hasLineCount(3)

193

.containsPattern("Line \\d");

194

195

String json = "{\"name\":\"John\",\"age\":30}";

196

assertThat(json)

197

.containsPattern("\"\\w+\":\\w+")

198

.startsWith("{")

199

.endsWith("}");

200

201

String normalizeTest = "Hello, World!";

202

assertThat(normalizeTest)

203

.isEqualToNormalizingPunctuationAndWhitespace("Hello World");

204

```

205

206

### String Transformations and Extractions

207

208

Transform strings for specialized assertions.

209

210

```java { .api }

211

// Case transformations

212

StringAssert asLowerCase()

213

StringAssert asUpperCase()

214

215

// Trimming

216

StringAssert trimmed()

217

218

// Decoding

219

StringAssert asBase64Decoded()

220

StringAssert asHexString()

221

StringAssert asUUID()

222

223

// Date parsing

224

DateAssert asDate()

225

LocalDateAssert asLocalDate()

226

LocalDateTimeAssert asLocalDateTime()

227

InstantAssert asInstant()

228

```

229

230

Usage examples:

231

```java

232

String upperText = "HELLO WORLD";

233

assertThat(upperText)

234

.asLowerCase()

235

.isEqualTo("hello world");

236

237

String paddedText = " trimmed ";

238

assertThat(paddedText)

239

.trimmed()

240

.isEqualTo("trimmed");

241

242

String dateString = "2023-12-25";

243

assertThat(dateString)

244

.asLocalDate()

245

.isAfter(LocalDate.of(2023, 12, 20));

246

247

String base64 = "SGVsbG8gV29ybGQ="; // "Hello World" encoded

248

assertThat(base64)

249

.asBase64Decoded()

250

.isEqualTo("Hello World");

251

```

252

253

### Unicode and Character Properties

254

255

Assertions for Unicode properties and character analysis.

256

257

```java { .api }

258

// Character content analysis

259

StringAssert containsOnlyDigits()

260

StringAssert containsOnlyLetters()

261

StringAssert containsOnlyLettersOrDigits()

262

StringAssert containsWhitespaces()

263

StringAssert doesNotContainAnyWhitespaces()

264

StringAssert containsOnlyWhitespaces()

265

266

// Unicode normalization

267

StringAssert isEqualToNormalizingUnicode(String expected)

268

```

269

270

Usage examples:

271

```java

272

String digits = "12345";

273

assertThat(digits).containsOnlyDigits();

274

275

String letters = "HelloWorld";

276

assertThat(letters).containsOnlyLetters();

277

278

String alphanumeric = "Hello123";

279

assertThat(alphanumeric).containsOnlyLettersOrDigits();

280

281

String withSpaces = "Hello World";

282

assertThat(withSpaces).containsWhitespaces();

283

284

String noSpaces = "HelloWorld";

285

assertThat(noSpaces).doesNotContainAnyWhitespaces();

286

```

287

288

### String Satisfies and Custom Validation

289

290

Custom validation logic for complex string requirements.

291

292

```java { .api }

293

// Custom string validation

294

StringAssert satisfies(Consumer<String> requirements)

295

StringAssert satisfiesAnyOf(Consumer<String>... requirements)

296

297

// String predicates

298

StringAssert matches(Predicate<String> predicate)

299

```

300

301

Usage examples:

302

```java

303

String password = "MySecurePass123!";

304

assertThat(password)

305

.satisfies(pwd -> {

306

assertThat(pwd.length()).isGreaterThanOrEqualTo(8);

307

assertThat(pwd).containsPattern("[A-Z]"); // uppercase

308

assertThat(pwd).containsPattern("[a-z]"); // lowercase

309

assertThat(pwd).containsPattern("\\d"); // digit

310

assertThat(pwd).containsPattern("[!@#$%^&*]"); // special

311

});

312

313

String filename = "document.pdf";

314

assertThat(filename)

315

.satisfies(name -> assertThat(name).endsWith(".pdf"))

316

.matches(name -> name.length() > 4);

317

```

318

319

## Types

320

321

```java { .api }

322

// Pattern for regex matching

323

class Pattern {

324

static Pattern compile(String regex)

325

static Pattern compile(String regex, int flags)

326

}

327

328

// Consumer for custom validation

329

interface Consumer<T> {

330

void accept(T t);

331

}

332

333

// Predicate for matching

334

interface Predicate<T> {

335

boolean test(T t);

336

}

337

338

// Character sequence interface

339

interface CharSequence {

340

int length()

341

char charAt(int index)

342

CharSequence subSequence(int start, int end)

343

String toString()

344

}

345

```