Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions grails-app/controllers/notepad/NotepadController.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class NotepadController {
def saveMethod() {

def saveNotes = new Notepad(params)
//TODO: System.out.* is imported automatically in Groovy - you can just use `println` - Zak
System.out.println("the value of params in controller = " + params) //scaffold
saveNotes.save()
redirect(action: 'current')
Expand All @@ -25,7 +26,9 @@ class NotepadController {

Notepad notepadInstance = Notepad.get(id)

//TODO: Can be shortened to if(!notepadInstance) - Google "Groovy truth" for details - Zak
if (notepadInstance == null) {
//TODO: System.out. is unnecessary
System.out.println("Docuemnt Object is NULL")
return
}
Expand Down
22 changes: 18 additions & 4 deletions src/test/groovy/notepad/NotepadControllerTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,29 @@ import java.sql.Statement;
@Mock(Notepad)
class NotepadControllerTest extends Specification {

//TODO: Unit tests should not be used to test against databases.
// GORM (the persistence framework used by Grails) is not running
// during unit tests. The proper place to test such functionality
// is within an *integration test*. https://docs.grails.org/latest/guide/testing.html#integrationTesting
//
// Also, even within an integration test, opening a direct JDBC
// connection is brittle and not usually necessary - you can simply
// use GORM to check for whether the record was persisted by calling
// Notepad.get() (or Notepad.list() to retrieve all of the records) - Zak

//A better use of unit tests for your controller might be (for example) to check
// if a redirect is issued when you expect (like your redirect to the 'current' action)

@Shared
Connection conn
Connection conn //TODO: As described above, you shouldn't use direct SQL connections
// in a unit test (or really any Grails test, usually)


@Shared
Statement stmt
Statement stmt //TODO: Same as above

@Shared
ResultSet rs
ResultSet rs //TODO: Same as above

/* Initialization logic here - connection over jdbc to h2 database*/
def setupSpec() {
Expand Down Expand Up @@ -79,7 +93,7 @@ class NotepadControllerTest extends Specification {

when:
controller.saveMethod()
viewDb()
viewDb() //TODO: As described above, persistence to the database will not work in a unit test

then:
response.redirectedUrl == "/notepad/current"
Expand Down