Skip to content

Instantly share code, notes, and snippets.

@takezoe
Created June 25, 2017 06:36
Show Gist options
  • Save takezoe/d3a14e502801a3e74f989232ae62130c to your computer and use it in GitHub Desktop.
Save takezoe/d3a14e502801a3e74f989232ae62130c to your computer and use it in GitHub Desktop.
Test case which uses @MockBean in Spring Boot application
package com.example.service;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.Collections;
import com.example.repository.CustomerRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CustomerServiceTest {
@Autowired
private CustomerService customerService;
@MockBean(name="customerRepository")
private CustomerRepository customerRepository;
@Test
public void test(){
when(customerRepository.findAll()).thenReturn(Collections.emptyList());
assertTrue(customerService.findAll().isEmpty());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment