Skip to content

Commit

Permalink
#40 optimized test for ClubBean#insertNewClub()
Browse files Browse the repository at this point in the history
  • Loading branch information
kbalt committed Mar 28, 2017
1 parent 0b5f364 commit 4084d28
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions src/test/java/de/rennspur/test/beans/ClubBeanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;

import static org.mockito.Mockito.*;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.MockitoJUnitRunner;
Expand Down Expand Up @@ -64,6 +62,17 @@ public void setUp() throws Exception {
when(emf.createEntityManager()).thenReturn(em);
when(em.createNamedQuery("Club.findAll")).thenReturn(q);
when(em.getTransaction()).thenReturn(et);
when(em.merge(any(Club.class))).then(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Club club = (Club) invocation.getArguments()[0];
assertEquals(proband.getName(), club.getName());
assertEquals(proband.getAbbreviation(), club.getAbreviation());
assertEquals(proband.getUrl(), club.getUrl());
assertEquals(proband.getDsv_number(), club.getDsvNumber());
return null;
}
});
}

/**
Expand All @@ -85,27 +94,15 @@ public void testIfInitInitializesFieldClubs() {
*/
@Test
public void testInsertNewClub() throws NoSuchMethodException, SecurityException {
// Check if return type is Type void
Class<? extends ClubBean> probandClass = proband.getClass();
Method insertNewClubMethod = probandClass.getMethod("insertNewClub");
assertTrue(insertNewClubMethod.getReturnType().equals(Void.TYPE));

when(em.merge(any())).then(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object obj = invocation.getArguments()[0];
if (obj instanceof Club) {
Club club = (Club) obj;
assertEquals(proband.getName(), club.getName());
assertEquals(proband.getAbbreviation(), club.getAbreviation());
assertEquals(proband.getUrl(), club.getUrl());
assertEquals(proband.getDsv_number(), club.getDsvNumber());
}
return null;
}
});
proband.insertNewClub();

// verify that the changes have been committed
verify(et, atLeast(1)).commit();
verify(em, atLeast(1)).close();
}

/**
Expand Down

0 comments on commit 4084d28

Please sign in to comment.