Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.nsu.fit.tm_backend.impl;
- import org.junit.jupiter.api.Test;
- import org.junit.jupiter.api.extension.ExtendWith;
- import org.mockito.InjectMocks;
- import org.mockito.Mock;
- import org.mockito.Mockito;
- import org.mockito.junit.jupiter.MockitoExtension;
- import org.nsu.fit.tm_backend.repository.Repository;
- import org.nsu.fit.tm_backend.repository.data.CustomerPojo;
- import org.nsu.fit.tm_backend.repository.data.SubscriptionPojo;
- import org.nsu.fit.tm_backend.service.CustomerService;
- import org.nsu.fit.tm_backend.service.SubscriptionService;
- import org.nsu.fit.tm_backend.service.data.StatisticBO;
- import org.nsu.fit.tm_backend.service.data.StatisticPerCustomerBO;
- import org.nsu.fit.tm_backend.service.impl.CustomerServiceImpl;
- import org.nsu.fit.tm_backend.service.impl.StatisticServiceImpl;
- import javax.inject.Inject;
- import java.util.*;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import static org.junit.jupiter.api.Assertions.assertTrue;
- import static org.mockito.Mockito.*;
- // Лабораторная 2: покрыть unit тестами класс StatisticServiceImpl на 100%.
- // Чтобы протестировать метод calculate() используйте Mockito.spy(statisticService) и переопределите метод
- // calculate(UUID customerId) чтобы использовать стратегию "разделяй и властвуй".
- @ExtendWith(MockitoExtension.class)
- public class StatisticServiceImplTest {
- @Mock
- private CustomerService customerService;
- @Mock
- private SubscriptionService subscriptionService;
- @InjectMocks
- private StatisticServiceImpl statisticService;
- CustomerPojo createMockCustomer(int balance) {
- CustomerPojo createCustomerInput = new CustomerPojo();
- createCustomerInput.firstName = "John";
- createCustomerInput.lastName = "Wick";
- createCustomerInput.login = "john_wick@example.com";
- createCustomerInput.pass = "Baba_Jaga";
- createCustomerInput.balance = balance;
- return createCustomerInput;
- }
- List<SubscriptionPojo> generateSubscriptions() {
- ArrayList<SubscriptionPojo> pojos = new ArrayList<>();
- SubscriptionPojo pojo1 = new SubscriptionPojo();
- pojo1.planFee = 10;
- pojo1.id = UUID.randomUUID();
- SubscriptionPojo pojo2 = new SubscriptionPojo();
- pojo2.planFee = 5;
- pojo2.id = UUID.randomUUID();
- pojos.add(pojo1);
- pojos.add(pojo2);
- return pojos;
- }
- @Test
- void TestCalculateByUUID() {
- List<SubscriptionPojo> subscriptions = generateSubscriptions();
- CustomerPojo sampleOutput = createMockCustomer(10);
- sampleOutput.id = UUID.randomUUID();
- when(customerService.lookupCustomer(sampleOutput.id)).thenReturn(sampleOutput);
- when(subscriptionService.getSubscriptions(sampleOutput.id)).thenReturn(subscriptions);
- StatisticPerCustomerBO output = statisticService.calculate(sampleOutput.id);
- // assert: проверяем результат выполнения метода.
- assertEquals(output.getOverallBalance(), sampleOutput.balance);
- assertEquals(output.getOverallFee(), 15);
- for (int i = 0; i < subscriptions.size(); ++i) {
- assertTrue(output.getSubscriptionIds().contains(subscriptions.get(i).id));
- }
- assertEquals(output.getSubscriptionIds().size(), subscriptions.size());
- verify(customerService, times(1)).lookupCustomer(sampleOutput.id);
- verify(subscriptionService, times(1)).getSubscriptions(sampleOutput.id);
- }
- @Test
- void TestCalculateNullCustomer() {
- CustomerPojo sampleOutput = createMockCustomer(10);
- sampleOutput.id = UUID.randomUUID();
- when(customerService.lookupCustomer(sampleOutput.id)).thenReturn(null);
- StatisticPerCustomerBO output = statisticService.calculate(sampleOutput.id);
- assertEquals(output, null);
- verify(customerService, times(1)).lookupCustomer(sampleOutput.id);
- }
- List<CustomerPojo> generateCustomers(int count) {
- ArrayList<CustomerPojo> pojos = new ArrayList<>();
- for (int i =0; i < count; ++i) {
- CustomerPojo customer = createMockCustomer(10);
- customer.id = UUID.randomUUID();
- pojos.add(customer);
- }
- return pojos;
- }
- List<StatisticPerCustomerBO> generateStatistics(int count) {
- ArrayList<StatisticPerCustomerBO> pojos = new ArrayList<>();
- for (int i =0; i < count; ++i) {
- StatisticPerCustomerBO statObjec = new StatisticPerCustomerBO();
- statObjec.setOverallBalance(10);
- statObjec.setOverallFee(5);
- pojos.add(statObjec);
- }
- return pojos;
- }
- @Test
- void testCalculate() {
- StatisticServiceImpl spyService = Mockito.spy(statisticService);
- List<CustomerPojo> customers = generateCustomers(4);
- List<StatisticPerCustomerBO> statistics = generateStatistics(4);
- Set<UUID> ids = new HashSet<>();
- for (int i=0;i<customers.size(); ++i) {
- statistics.get(i).setCustomerId(customers.get(i).id);
- ids.add(customers.get(i).id);
- Mockito.doReturn(statistics.get(i)).when(spyService).calculate(customers.get(i).id);
- //when(service.calculate(customers.get(i).id)).thenReturn(statistics.get(i));
- }
- // test case of null
- UUID randomId = UUID.randomUUID();
- ids.add(randomId);
- Mockito.doReturn(null).when(spyService).calculate(randomId);
- when(customerService.getCustomerIds()).thenReturn(ids);
- StatisticBO bo = spyService.calculate();
- assertEquals(bo.getOverallBalance(), 40);
- assertEquals(bo.getOverallFee(), 20);
- assertEquals(bo.getCustomers().size(), statistics.size());
- for (int i = 0; i < statistics.size(); ++i) {
- assertTrue(bo.getCustomers().contains(statistics.get(i)));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement