Created
June 25, 2017 06:36
-
-
Save takezoe/d3a14e502801a3e74f989232ae62130c to your computer and use it in GitHub Desktop.
Test case which uses @MockBean in Spring Boot application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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