Skip to content

Instantly share code, notes, and snippets.

@sdelamo
Created September 7, 2017 16:06
Show Gist options
  • Save sdelamo/c65434a3bace81f8309adc5c4e41098d to your computer and use it in GitHub Desktop.
Save sdelamo/c65434a3bace81f8309adc5c4e41098d to your computer and use it in GitHub Desktop.
Don't do this, call save when you want to update or save
package demo
class Note {
String msg
}
package demo
import grails.gorm.transactions.ReadOnly
import grails.gorm.transactions.Transactional
import groovy.transform.CompileStatic
@CompileStatic
class NoteService {
@ReadOnly
Note read(Serializable id) {
Note.read(id)
}
@Transactional
Note saveNote(String msg) {
Note note = new Note(msg: msg)
note.save()
}
@Transactional
Note updateMsg(Serializable id, String msg) {
Note note = Note.get(id)
note.msg = msg
note
}
}
package demo
import grails.testing.mixin.integration.Integration
import org.springframework.beans.factory.annotation.Autowired
import spock.lang.Specification
@Integration
class NoteSpec extends Specification {
@Autowired
NoteService noteService
def "test without calling save"() {
when:
Note note = noteService.saveNote('Note A')
Serializable noteId = note.id
then:
noteService.read(noteId).msg == 'Note A'
when:
noteService.updateMsg(noteId, 'Note B')
then:
noteService.read(noteId).msg == 'Note B'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment