Spring Boot Mockito Support: Difference between revisions

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


Mocks will be automatically reset across tests.
Mocks will be automatically reset across tests.
=Dependencies=
<syntaxhighlight lang='groovy'>
dependencies {
  ...
  testImplementation 'org.springframework.boot:spring-boot-starter-test'
  testImplementation 'org.mockito:mockito-core:3.12.4'
}
</syntaxhighlight>


=Example=
=Example=

Revision as of 01:17, 3 October 2021

External

Internal

Overview

@MockBean

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");
    }
}