使用Mockito测试Spring-Boot Repository接口方法而不触及数据库

Den*_* M. 5 java mockito spring-boot

我有以下测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class TransactionServiceTests {

    @Rule
    public MockitoRule mockitoRule = MockitoJUnit.rule();

    @Mock
    private MessagingService mockMessagingService;
    @Mock
    private CustomerRepository mockCustomerRepository;

    @Autowired
    TransactionService transactionService;

    @Test
    public void testTransactionBetweenCustomersAndBalanceOfReceiver() {

        int AMOUNT = 50;

        // prepare your test data unless you always expect those values to exist.
        Customer customerReceiver = new Customer();
        customerReceiver.setName("TestReceiver");
        customerReceiver.setBalance(12);
        mockCustomerRepository.save(customerReceiver);

        Customer customerSender = new Customer();
        customerSender.setName("TestSender");
        customerSender.setBalance(50);
        mockCustomerRepository.save(customerSender);

        int expectedReceiverAmount = customerReceiver.getBalance() + AMOUNT;
        int expectedSenderAmount = customerSender.getBalance() - AMOUNT;
        transactionService.makeTransactionFromSenderToReceiver(customerSender, customerReceiver, AMOUNT);

        assertEquals(expectedSenderAmount, customerSender.getBalance());
        assertEquals(expectedReceiverAmount, customerReceiver.getBalance());

    }
}
Run Code Online (Sandbox Code Playgroud)

这是TransactionService.类本身:

@Service
public class TransactionService {

    private MessagingService messagingService;
    private CustomerRepository customerRepository;

    private static final Logger log = LoggerFactory.getLogger(TransactionService.class);

    @Autowired
    public TransactionService(MessagingService messagingService, CustomerRepository customerRepository){
        Assert.notNull(messagingService, "MessagingService cannot be null");
        this.messagingService = messagingService;
        Assert.notNull(customerRepository, "CustomerRepository cannot be null");
        this.customerRepository = customerRepository;
    }

    public void makeTransactionFromSenderToReceiver(Customer sender, Customer receiver, int amount) {

        if (sender.getBalance() >= amount) {
            sender.setBalance(sender.getBalance() - amount);
            receiver.setBalance(receiver.getBalance() + amount);

            customerRepository.save(sender);
            customerRepository.save(receiver);
        }

        else {    
            throw new RuntimeException();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在测试期间,它将上述用户添加到我的实时数据库中,并在测试完成后将其留在那里.我能以某种方式告诉Mockito不要触摸我的数据库吗?或者这完全不可能?

Bra*_*zic 5

"模拟"您的存储库方法调用.另外,请@InjectMocks改为@Autowired使用TransactionService.你也可以使用MockitoJUnitRunner.如何模拟存储库调用:

when(customerRepository.save(sender)).thenReturn(someSenderInstance);
Run Code Online (Sandbox Code Playgroud)

要验证是否已调用mocked方法调用,请使用:

verify(customerRepository, times(1)).save(sender);
Run Code Online (Sandbox Code Playgroud)

另外,请记住一件事:您正在测试服务!因此,应该模拟对数据库的所有调用.