Spring Boot Mockito Support
Jump to navigation
Jump to search
External
- https://www.baeldung.com/injecting-mocks-in-spring
- https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4#mocking-and-spying
Internal
Overview
Mocks will be automatically reset across tests.
Dependencies
dependencies {
...
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.mockito:mockito-core:3.12.4'
}
Example
@RunWith(SpringRunner.class)
@SpringBootTest
public class SomeTest {
@Autowired
private TestRestTemplate restTemplate;
@MockBean
private SomeService someService;
@Before
public void setup() {
// this stubs behavior
given(someService.getSomething("blah")).willReturn(new Something("blah"));
}
@Test
public void test() {
restTemplate.getForEntity("/{username}/something, String.class, "blue");
}
}