CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-scala-js--scalajs-junit-test-runtime

JUnit test runtime for Scala.js - provides the runtime infrastructure for running JUnit tests compiled with Scala.js

Pending
Overview
Eval results
Files

core-assertions.mddocs/

Core Assertions

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.

Basic Assertions

Boolean Assertions

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

Usage:

assertTrue(user.isActive)
assertTrue("User should be active", user.isActive)
assertFalse(result.isEmpty)
assertFalse("Result should not be empty", result.isEmpty)

Equality Assertions

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

Usage:

assertEquals(42, calculator.add(20, 22))
assertEquals("Addition failed", 42, calculator.add(20, 22))
assertNotEquals(0, user.getId())

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

Numeric Equality with Tolerance

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

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

Usage:

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

Type-Specific Equality

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

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

Null Assertions

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

Usage:

assertNull(user.getManager())
assertNull("Manager should be null for new users", user.getManager())
assertNotNull(user.getName())
assertNotNull("User name is required", user.getName())

Reference Equality

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

Usage:

val singleton = DatabaseConnection.getInstance()
assertSame(singleton, DatabaseConnection.getInstance())
assertNotSame(user1, user2)

Exception Testing

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

Usage:

val exception = assertThrows(classOf[IllegalArgumentException], () => {
  calculator.divide(10, 0)
})
assertEquals("Division by zero", exception.getMessage)

assertThrows("Should throw on negative input", classOf[IllegalArgumentException], () => {
  validator.validateAge(-5)
})

Hamcrest Integration

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

Usage:

import org.hamcrest.CoreMatchers._

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

Forced Failures

def fail(): Unit
def fail(message: String): Unit

Usage:

if (unexpectedCondition) {
  fail("This should never happen")
}

try {
  dangerousOperation()
  fail() // Should have thrown exception
} catch {
  case _: ExpectedException => // Expected
}

Error Message Formatting

The Assert object provides enhanced error message formatting:

  • String comparison: Automatic diff highlighting for string mismatches
  • Object comparison: Class and value information when toString() values match
  • Array comparison: Index-specific error reporting for array mismatches
  • Null handling: Clear indication of expected vs actual null values

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

Install with Tessl CLI

npx tessl i tessl/maven-org-scala-js--scalajs-junit-test-runtime

docs

array-assertions.md

core-assertions.md

exception-handling.md

hamcrest-matchers.md

index.md

test-assumptions.md

test-lifecycle.md

test-runners.md

tile.json