Java Inline Approvals
Inline approvals store expected output directly in test code using Java text blocks (""").
Contents
- Basic Usage
- InlineOptions Modes
- With Code Display
- Kotlin Support
- How It Works
- When to Use Inline vs File Approvals
Basic Usage
import org.approvaltests.Approvals;
import org.approvaltests.core.Options;
@Test
void testGreeting() {
var expected = """
Hello World!
Welcome to inline approvals!
""";
Approvals.verify(getGreeting(), new Options().inline(expected));
}When the test fails, the framework automatically updates expected with the actual output.
InlineOptions Modes
Automatic Mode
Auto-approves and updates source code immediately:
var expected = "";
Approvals.verify(actual, new Options().inline(expected, InlineOptions.automatic()));Semi-Automatic Mode
Adds a marker that must be manually deleted to approve:
var expected = """
actual output here
***** DELETE ME TO APPROVE *****
""";
Approvals.verify(actual, new Options().inline(expected, InlineOptions.semiAutomatic()));Delete the ***** DELETE ME TO APPROVE ***** line to approve.
Semi-Automatic with Previous
Shows previous approved value for comparison:
var expected = """
new output
vvvvv PREVIOUS RESULT vvvvv
old output
""";
Approvals.verify(actual, new Options().inline(expected,
InlineOptions.semiAutomaticWithPreviousApproved()));With Code Display
Show source code context in diff:
Approvals.verify(actual, new Options().inline(expected, InlineOptions.showCode(true)));Kotlin Support
Uses Kotlin string templates with trimIndent():
@Test
fun `test greeting`() {
val expected = """
Hello World!
Welcome to inline approvals!
""".trimIndent()
Approvals.verify(getGreeting(), Options().inline(expected))
}How It Works
- Test fails →
InlineJavaReporteractivates - Reporter finds test method via stack trace
- Parses source file and locates the
expectedvariable - Replaces the text block content with actual output
- Rewrites source file
When to Use Inline vs File Approvals
Use inline when:
- Output is short (few lines)
- You want test and expectation in one place
- Quick iteration during TDD
Use file approvals when:
- Output is long or complex
- Multiple tests share similar output patterns
- Binary content (images, PDFs)
- JSON/XML that benefits from syntax highlighting