or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

array-assertions.mdcore-assertions.mdexception-handling.mdhamcrest-matchers.mdindex.mdtest-assumptions.mdtest-lifecycle.mdtest-runners.md

core-assertions.mddocs/

0

# Core Assertions

1

2

The Assert object provides the fundamental assertion methods for JUnit test validation. All assertion methods support optional custom error messages and throw AssertionError when conditions fail.

3

4

## Basic Assertions

5

6

### Boolean Assertions

7

8

```scala { .api }

9

def assertTrue(condition: Boolean): Unit

10

def assertTrue(message: String, condition: Boolean): Unit

11

def assertFalse(condition: Boolean): Unit

12

def assertFalse(message: String, condition: Boolean): Unit

13

```

14

15

**Usage:**

16

```scala

17

assertTrue(user.isActive)

18

assertTrue("User should be active", user.isActive)

19

assertFalse(result.isEmpty)

20

assertFalse("Result should not be empty", result.isEmpty)

21

```

22

23

### Equality Assertions

24

25

```scala { .api }

26

def assertEquals(expected: Any, actual: Any): Unit

27

def assertEquals(message: String, expected: Any, actual: Any): Unit

28

def assertNotEquals(unexpected: Any, actual: Any): Unit

29

def assertNotEquals(message: String, unexpected: Any, actual: Any): Unit

30

```

31

32

**Usage:**

33

```scala

34

assertEquals(42, calculator.add(20, 22))

35

assertEquals("Addition failed", 42, calculator.add(20, 22))

36

assertNotEquals(0, user.getId())

37

```

38

39

**String Comparison:** For String values, assertEquals automatically generates ComparisonFailure with detailed diff information.

40

41

### Numeric Equality with Tolerance

42

43

```scala { .api }

44

def assertEquals(expected: Double, actual: Double, delta: Double): Unit

45

def assertEquals(message: String, expected: Double, actual: Double, delta: Double): Unit

46

def assertEquals(expected: Float, actual: Float, delta: Float): Unit

47

def assertEquals(message: String, expected: Float, actual: Float, delta: Float): Unit

48

49

def assertNotEquals(unexpected: Double, actual: Double, delta: Double): Unit

50

def assertNotEquals(message: String, unexpected: Double, actual: Double, delta: Double): Unit

51

def assertNotEquals(unexpected: Float, actual: Float, delta: Float): Unit

52

def assertNotEquals(message: String, unexpected: Float, actual: Float, delta: Float): Unit

53

```

54

55

**Usage:**

56

```scala

57

assertEquals(3.14159, Math.PI, 0.001)

58

assertEquals("Pi approximation", 3.14159, Math.PI, 0.001)

59

assertNotEquals(0.0, result, 0.001)

60

```

61

62

### Type-Specific Equality

63

64

```scala { .api }

65

def assertEquals(expected: Int, actual: Int): Unit

66

def assertEquals(message: String, expected: Int, actual: Int): Unit

67

def assertEquals(expected: Long, actual: Long): Unit

68

def assertEquals(message: String, expected: Long, actual: Long): Unit

69

70

def assertNotEquals(unexpected: Int, actual: Int): Unit

71

def assertNotEquals(message: String, unexpected: Int, actual: Int): Unit

72

def assertNotEquals(unexpected: Long, actual: Long): Unit

73

def assertNotEquals(message: String, unexpected: Long, actual: Long): Unit

74

```

75

76

## Null Assertions

77

78

```scala { .api }

79

def assertNull(obj: Any): Unit

80

def assertNull(message: String, obj: Any): Unit

81

def assertNotNull(obj: Any): Unit

82

def assertNotNull(message: String, obj: Any): Unit

83

```

84

85

**Usage:**

86

```scala

87

assertNull(user.getManager())

88

assertNull("Manager should be null for new users", user.getManager())

89

assertNotNull(user.getName())

90

assertNotNull("User name is required", user.getName())

91

```

92

93

## Reference Equality

94

95

```scala { .api }

96

def assertSame(expected: Any, actual: Any): Unit

97

def assertSame(message: String, expected: Any, actual: Any): Unit

98

def assertNotSame(unexpected: Any, actual: Any): Unit

99

def assertNotSame(message: String, unexpected: Any, actual: Any): Unit

100

```

101

102

**Usage:**

103

```scala

104

val singleton = DatabaseConnection.getInstance()

105

assertSame(singleton, DatabaseConnection.getInstance())

106

assertNotSame(user1, user2)

107

```

108

109

## Exception Testing

110

111

```scala { .api }

112

def assertThrows[T <: Throwable](expectedThrowable: Class[T], runnable: ThrowingRunnable): T

113

def assertThrows[T <: Throwable](message: String, expectedThrowable: Class[T], runnable: ThrowingRunnable): T

114

```

115

116

**Usage:**

117

```scala

118

val exception = assertThrows(classOf[IllegalArgumentException], () => {

119

calculator.divide(10, 0)

120

})

121

assertEquals("Division by zero", exception.getMessage)

122

123

assertThrows("Should throw on negative input", classOf[IllegalArgumentException], () => {

124

validator.validateAge(-5)

125

})

126

```

127

128

## Hamcrest Integration

129

130

```scala { .api }

131

def assertThat[T](actual: T, matcher: Matcher[T]): Unit

132

def assertThat[T](reason: String, actual: T, matcher: Matcher[T]): Unit

133

```

134

135

**Usage:**

136

```scala

137

import org.hamcrest.CoreMatchers._

138

139

assertThat(user.getName(), is(notNullValue()))

140

assertThat("User should be adult", user.getAge(), is(not(lessThan(18))))

141

```

142

143

## Forced Failures

144

145

```scala { .api }

146

def fail(): Unit

147

def fail(message: String): Unit

148

```

149

150

**Usage:**

151

```scala

152

if (unexpectedCondition) {

153

fail("This should never happen")

154

}

155

156

try {

157

dangerousOperation()

158

fail() // Should have thrown exception

159

} catch {

160

case _: ExpectedException => // Expected

161

}

162

```

163

164

## Error Message Formatting

165

166

The Assert object provides enhanced error message formatting:

167

168

- **String comparison**: Automatic diff highlighting for string mismatches

169

- **Object comparison**: Class and value information when toString() values match

170

- **Array comparison**: Index-specific error reporting for array mismatches

171

- **Null handling**: Clear indication of expected vs actual null values

172

173

All assertion methods are marked with `@noinline` to preserve stack trace information for better debugging.