jdlib / deepdive

Fluent assertions library for Java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Deep Dive Assertions

Deep Dive is an assertion library for Java. It offers a fluent API which allows you to dive deep, i.e. going back and forth between different assertion objects.

TL;DR

Tests written with Deep Dive look like this:

import java.io.File;
import static java.nio.StandardCharsets.UTF_8;
import static deepdive.ExpectThat.expectThat;		
	
// inside a test method:		
File dir = ...                     // dir is a File object which we expect to be a directory  
expectThat(dir)                    // starting with a deepdive.actual.io.FileActual to test the directory
    .isDirectory()                 // test a File property
    .name()                        // dive into a deepdive.actual.lang.StringActual to test the File name
    	.startsWith("test_")       // test a File name property
    	.endsWith("_files")        // test a File name property
    	.back()                    // back to the FileActual
    .set().childFile("result.txt") // replace the actual File value with a child File
    .exists()                      // test a File property
    .isFile()                      // test a File property
    .not().isHidden()              // negate any available assertion by a preceding not() 
    .length()                      // dive into a deepdive.actual.lang.LongActual to the test the file length
        .greater(50)               // test a File length property
        .back()                    // back to the FileActual
    .read(UTF_8).lineIterator()    // read file content as lines and dive into a deepdive.actual.util.StringIteratorActual 
        .next()                    // dive into a StringActual for the first line 
            .startsWith("Hold your breath") // tests a line property
            .back()                // back to the StringIteratorActual
        .skip(5)                   // skip 5 lines
        .next()                    // dive into a StringActual to test the next line
            .isLowerCase()         // test a line property
            .back()                // back to the StringIteratorActual
    	.not().hasNext();          // test for iteration end: done!

License

Deep Dive can be used under the terms of the Gnu Public License v3 or the Apache 2.0 license, see License.md for details.

Dependencies

Deep Dive requires Java 8+ and has no external dependencies. It works great with test engines like JUnit or TestNG, just transition over from JUnit or TestNG assertions to the ones offered by Deep Dive.

How to use

Download the latest release and include the Deep Dive jar into your classpath. The user guide explains the details on how to use Deep Dive.

Why to use

Like other Java assertions libraries (e.g. FEST Assert, AssertJ and Google Truth) Deep Dive provides a fluent API to state assertions. But it goes beyond those libraries

  • by allowing to dive deep, i.e. going back and forth between different assertion objects,
  • providing not-mode to easily negate any assertion provided by the API,
  • therefore resulting in a small library (~250K) with great assertion power.

For further details please dive into Motivation.md.

Limitations

Deep Dive makes heavy use of type parameters and recursive type bounds. The Eclipse Compiler for Java (ECJ) used by Eclipse IDE seems to be challenged by this. Therefore you may experience compile errors when diving too deep into assertion objects. javac from the JDK is not affected.

About

Fluent assertions library for Java

License:Other


Languages

Language:Java 97.9%Language:CSS 2.1%