본문 바로가기

Spring & Java

Spring boot @MockBean

반응형
@MockBean
- includes Mockito @Mock functionality
- also adds mock bean to
    if existing bean is there, the mock bean will replace it
- thus making the mock bean available for injection with @Autowired

Using @Mock

@SpringBootTest
class StudentRepositoryTest {
    @Mock
    private StudentRepository studentRepository;
    @InjectMocks
    private StudentService studentService;
}

Using @MockBean

@SpringBootTest
class StudentRepositoryTest {
    //Create mock for the repository
    @MockBean
    private StudentRepository studentRepository;
    
    //inject dependencies 
    @Autowired
    private StudentSersvice studentService;
    
}
반응형