Skip to content

Commit

Permalink
better RevisionServiceTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Venca24 committed Jan 14, 2017
1 parent 2f6f0e4 commit 0da47b0
Showing 1 changed file with 15 additions and 34 deletions.
49 changes: 15 additions & 34 deletions service/src/test/java/service/RevisionServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import service.config.ServiceConfiguration;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
Expand All @@ -57,7 +55,6 @@ public void setup() throws ServiceException
private Machine testMachine;
private User testUser;

@BeforeMethod
public void prepareTestMachine()
{
testMachine = new Machine();
Expand All @@ -73,7 +70,6 @@ public void prepareTestMachine()
testMachine.setMachineType(MachineType.EXCAVATOR);
}

@BeforeMethod
public void prepareTestUser()
{
testUser = new User();
Expand All @@ -87,6 +83,9 @@ public void prepareTestUser()
@BeforeMethod
public void prepareTestRevision()
{
prepareTestMachine();
prepareTestUser();

testRevision = new Revision();

Calendar cal = Calendar.getInstance();
Expand All @@ -101,54 +100,36 @@ public void prepareTestRevision()

@Test
public void testCreate() {
Long id = 1L;
doAnswer(invocation -> {
Object arg = invocation.getArguments()[0];
Revision revision = (Revision) arg;
revision.setId(id);
return null;
}).when(revisionDao).create(any(Revision.class));

revisionService.create(testRevision);
org.testng.Assert.assertEquals(id, testRevision.getId());
verify(revisionDao, times(1)).create(testRevision);
}

@Test
public void testUpdate() {
testRevision.setId(2L);
when(revisionService.findById(2L)).thenReturn(testRevision);
doAnswer(invocation -> {
Object arg = invocation.getArguments()[0];
Revision revision = (Revision) arg;
return revision;
}).when(revisionDao).update(any(Revision.class));
testRevision.setInfo("Test");
revisionService.update(testRevision);
Revision updated = revisionService.findById(2L);
Assert.assertEquals(testRevision.getInfo(), updated.getInfo());
verify(revisionDao, times(1)).update(testRevision);
}

@Test
public void testDelete() {
testRevision.setId(1L);
when(revisionService.findById(eq(testRevision.getId()))).thenReturn(testRevision).thenReturn(null);
Assert.assertNotNull(revisionService.findById(testRevision.getId()));
revisionService.delete(testRevision);
Assert.assertNull(revisionService.findById(testRevision.getId()));
verify(revisionDao, times(1)).delete(testRevision);
}

@Test
public void findById() {
when(revisionService.findById(2L)).thenReturn(testRevision);

Assert.assertEquals(revisionService.findById(2L), testRevision);
Assert.assertNull(revisionService.findById(-5L));
revisionService.findById(2L);
verify(revisionDao, times(1)).findById(2L);
}

@Test
public void findAllRentals() {
when(revisionService.findAllRevisions()).thenReturn(Arrays.asList(testRevision));
Assert.assertEquals(revisionService.findAllRevisions().size(), 0);

when(revisionDao.findAllRevisions()).thenReturn(Arrays.asList(testRevision));

Assert.assertEquals(1, revisionService.findAllRevisions().size());
Assert.assertEquals(revisionService.findAllRevisions().size(), 1);
}
}
}

0 comments on commit 0da47b0

Please sign in to comment.