Prawidłowe mockowanie w testach

0

Czy ktoś mógłby mi wyjaśnić jak prawidłowo wykonać tutaj mockowanie.
W obecnej sytuacji chciałbym zmockować metodę

public Account findAccountByUsername(String username)

interfejsu AccountService aby zwracała obiekt newAccount. Wobec tego zrobiłem: given(this.accountService.findAccountByUsername(newAccount.getUsername())).willReturn(newAccount);

No i tutaj pojawia się problem, bo to mockowanie działa tylko bezpośrednio w teście, tzn jak wykonam 
```java
this.accountService.findAccountByUsername(newAccount.getUsername())

to to faktycznie zwróci obiekt newAccount, ale już w momencie wykonywania testu jak sterowanie przechodzi do kontrolera to w nim już nie ma mockowanie przez co w linii Account account = accountService.findAccountByUsername(username);

 mam NULLa i cały test się sypie w <code class="java">.andExpect(model().attributeExists("account"))

i wyrzuca błąd java.lang.AssertionError: Model attribute 'account' does not exist

Jak to prawidłowo ogranąć?

Mój przykładowy test:
```java
@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration(classes = ApplicationContextConfiguration.class)
@WebAppConfiguration
@Transactional
public class TestAccountController {

    @Mock
    private AccountService accountService;

    @Mock
    private UsernameValidator usernameValidator;

    @InjectMocks
    private AccountController accountController;

    private MockMvc mockMvc;

    private Account newAccount;

    @Before
    public void setUp() {
        newAccount = new Account();
        newAccount.setId(1L);
        newAccount.setUsername("user");
        newAccount.setPassword("pass");
        newAccount.setEnabled(true);
        newAccount.setEmail("[email protected]");
        newAccount.setDateCreated(new Date());
        newAccount.setRole(Account.ROLE_USER);

        this.mockMvc = MockMvcBuilders.standaloneSetup(this.accountController)
                .build();

        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testInitUpdateAccountForm() throws Exception {
        given(this.accountService.findAccountByUsername(newAccount.getUsername())).willReturn(newAccount);

        mockMvc.perform(get("/accounts/edit/{username}", newAccount.getUsername()))
                .andExpect(status().isOk())
                .andExpect(model().attributeExists("account"))
                .andExpect(view().name("accounts/accountData"));
    }
}

Mój przykładowy controller:

@Controller
@RequestMapping(value = "/accounts")
public class AccountController {

    private static final String VIEWS_ACCOUNT_FORM = "accounts/accountData";
    private final AccountService accountService;
    private final UsernameValidator usernameValidator;

    @Autowired
    public AccountController(AccountService accountService, UsernameValidator usernameValidator) {
        this.accountService = accountService;
        this.usernameValidator = usernameValidator;
    }

    @RequestMapping(value = "/edit/{username}", method = RequestMethod.GET)
    public String initUpdateAccountForm(@PathVariable String username, Model model) {

        Account account = accountService.findAccountByUsername(username);
        model.addAttribute("account", account);
        return VIEWS_ACCOUNT_FORM;
    }
}
2
  1. Wstrzyknij mocki przez konstruktor zamiast @InjectMocks
  2. Stosowanie MockitoJUnitRunner.class i initMocks(this) jednocześnie jest nadmiarowe, wybierz jedno
  3. Nie wygląda by @WebAppConfiguration i @Transactional były potrzebne

1 użytkowników online, w tym zalogowanych: 0, gości: 1