SHOW:
|
|
- or go back to the newest paste.
1 | package com.deious.service.account; | |
2 | ||
3 | import com.deious.domain.account.Account; | |
4 | import com.deious.repository.account.AccountRepository; | |
5 | import com.prociss.paragon.testing.specification.AfterInvocation; | |
6 | import com.prociss.paragon.testing.specification.BeforeInvocation; | |
7 | import com.prociss.paragon.testing.specification.InvalidInvocation; | |
8 | import com.prociss.paragon.testing.specification.SpecificationCallback; | |
9 | import com.prociss.paragon.testing.specification.ValidInvocation; | |
10 | import org.junit.Before; | |
11 | import org.junit.Test; | |
12 | import org.junit.runner.RunWith; | |
13 | import org.mockito.ArgumentCaptor; | |
14 | import org.mockito.Mock; | |
15 | import org.mockito.invocation.InvocationOnMock; | |
16 | import org.mockito.runners.MockitoJUnitRunner; | |
17 | import org.mockito.stubbing.Answer; | |
18 | ||
19 | import static com.prociss.paragon.testing.specification.Specifications.emailSpec; | |
20 | import static com.prociss.paragon.testing.specification.ValidationVerifier.specification; | |
21 | import static org.fest.assertions.Assertions.assertThat; | |
22 | import static org.mockito.Matchers.any; | |
23 | import static org.mockito.Mockito.reset; | |
24 | import static org.mockito.Mockito.verify; | |
25 | import static org.mockito.Mockito.verifyZeroInteractions; | |
26 | import static org.mockito.Mockito.when; | |
27 | ||
28 | @RunWith(MockitoJUnitRunner.class) | |
29 | public class AccountServiceTest { | |
30 | ||
31 | @Mock | |
32 | private AccountRepository accountRepository; | |
33 | ||
34 | private AccountService accountService; | |
35 | ||
36 | @Before | |
37 | public void setupService() { | |
38 | this.accountService = new AccountService(accountRepository); | |
39 | } | |
40 | ||
41 | @Test | |
42 | public void verifyCreateAccountParameterValidation() throws Throwable { | |
43 | ||
44 | specification( | |
45 | accountService, | |
46 | "createAccount", | |
47 | Account.class, | |
48 | new CreateAccountCallback(), | |
49 | emailSpec().notNull()); | |
50 | ||
51 | } | |
52 | ||
53 | public class CreateAccountCallback implements SpecificationCallback { | |
54 | ||
55 | @BeforeInvocation | |
56 | public void setupMock() { | |
57 | ||
58 | - | when(accountRepository.save(any(Account.class))).thenAnswer(new Answer<Object>() { |
58 | + | when(accountRepository.save(any(Account.class))).thenAnswer(new Answer<Account>() { |
59 | @Override | |
60 | - | public Object answer(InvocationOnMock invocation) throws Throwable { |
60 | + | public Account answer(InvocationOnMock invocation) throws Throwable { |
61 | Account account = (Account) invocation.getArguments()[0]; | |
62 | return new Account(account.getEmail()); | |
63 | } | |
64 | }); | |
65 | } | |
66 | ||
67 | @ValidInvocation | |
68 | public void validEmail(final String validEmail, final Account returnedAccount) { | |
69 | ||
70 | ArgumentCaptor<Account> accountCaptor = ArgumentCaptor.forClass(Account.class); | |
71 | verify(accountRepository).save(accountCaptor.capture()); | |
72 | ||
73 | Account savedAccount = accountCaptor.getValue(); | |
74 | ||
75 | assertThat(savedAccount.getEmail()).isEqualTo(validEmail); | |
76 | assertThat(returnedAccount.getEmail()).isEqualTo(validEmail); | |
77 | } | |
78 | ||
79 | @InvalidInvocation | |
80 | public void invalidEmail(final String invalidEmail) { | |
81 | ||
82 | verifyZeroInteractions(accountRepository); | |
83 | ||
84 | } | |
85 | ||
86 | @AfterInvocation | |
87 | public void resetMock() { | |
88 | ||
89 | reset(accountRepository); | |
90 | } | |
91 | ||
92 | ||
93 | } | |
94 | ||
95 | } |