SampleDao { public void createSample(final Sample sample, Long userIdMakingChange) { Database db = dbp.get(); Date updateTime = db.nowPerApp(); Long sampleId = db.toInsert( "insert into sample (sample_id, sample_name, update_sequence, update_time) values (?,?,0,?)") .argPkSeq("id_seq") .argString(sample.getName()) .argDate(updateTime) .insertReturningPkSeq("sample_id"); db.toInsert("insert into sample_history (sample_id, sample_name, update_sequence, update_time, update_user_id," + " is_deleted) values (?,?,0,?,?,'N')") .argLong(sampleId) .argString(sample.getName()) .argDate(updateTime) .argLong(userIdMakingChange) .insert(1); sample.setSampleId(sampleId); sample.setUpdateSequence(0); sample.setUpdateTime(updateTime); } SampleDao(Supplier<Database> dbp); void createSample(final Sample sample, Long userIdMakingChange); Sample findSampleById(final Long sampleId, boolean lockRow); void updateSample(Sample sample, Long userIdMakingChange); void deleteSample(Sample sample, Long userIdMakingChange);  }
@Test public void testCreate() throws Exception { when(db.insertReturningPk(anyString(), anyString())).thenReturn(1L); Sample sample = new Sample(); sample.setName("Foo"); sampleDao.createSample(sample, 1L); assertEquals(new Long(1L), sample.getSampleId()); assertEquals("Foo", sample.getName()); assertEquals(new Integer(0), sample.getUpdateSequence()); assertEquals(now, sample.getUpdateTime()); verify(db).insertReturningPk(eq("insert into sample (sample_id, sample_name, update_sequence, update_time) values (nextval('id_seq'),?,0,?)"), anyString()); verify(db).insert(eq("insert into sample_history (sample_id, sample_name, update_sequence, update_time, update_user_id, is_deleted) values (?,?,0,?,?,'N')"), anyString()); verifyNoMoreInteractions(db); }