Writing your first test

Locate the root package of your unit tests and create a new class called NoteTest as follows:

    package com.journaler 
 
    import android.location.Location 
    import com.journaler.database.Content 
    import com.journaler.model.Note 
    import org.junit.Test 
 
    class NoteTest { 
 
      @Test 
      fun noteTest() { 
        val note = Note( 
                "stub ${System.currentTimeMillis()}", 
                "stub ${System.currentTimeMillis()}", 
                Location("Stub") 
        ) 
 
        val id = Content.NOTE.insert(note) 
        note.id = id 
 
        assert(note.id > 0) 
     } 
    } 

The test is very simple. It creates a new instance of Note, triggers the CRUD operation in our content provider to store it, and verifies the ID received. To run the test, right-click on class from the Project pane and choose Run 'NoteTest':

The unit test ...

Get Mastering Android Development with Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.