Spring Boot Mockito Support: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 10: Line 10:


[[@MockBean]]
[[@MockBean]]
=Example=
<syntaxhighlight lang='java'>
@RunWith(SpringRunner.class)
@SpringBootTest
public class SomeTest {
    @Autowired
    private TestRestTemplate restTemplate;
    @MockBean
    private SomeService someService;
    @Before
    public void setup() {
        given(someService.getSomething("blah")).willReturn(
            new Something("blah"));
    }
    @Test
    public void test() {
        restTemplate.getForEntity("/{username}/something, String.class, "blue");
    }
}
</syntaxhighlight>

Revision as of 00:37, 11 October 2018

Internal

Overview

TODO: https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4#mocking-and-spying

@MockBean

Example

@RunWith(SpringRunner.class)
@SpringBootTest
public class SomeTest {

    @Autowired
    private TestRestTemplate restTemplate;

    @MockBean
    private SomeService someService;

    @Before
    public void setup() {

        given(someService.getSomething("blah")).willReturn(
            new Something("blah"));
    }

    @Test
    public void test() {

        restTemplate.getForEntity("/{username}/something, String.class, "blue");
    }
}