Blue Vibes Test Core
Provides common test features for testing bv modules as well as micro applications.
1. Slicing context
In order to make unit test more efficient, developer should not load whole application context. Instead of it a sliced context with required beans should be used. To make writing unit test more elegant, bv offers annotation @TestWithContext
, which represents composition of multiple spring annotation. To create a spring unit test with sliced context it’s enough to mark class with this annotation. Example:
@TestWithContext({ExampleService.class})
class UnitTest {
@Autowired
private ExampleService service;
@Test
void testCase(){
//...
}
}
2. Utils
Provides set of utils that help developer to write test easier end more elegant:
MockitoUtils
- sugar code methods for mockitoResultActionAssert
(deprecated) - fluent assertion for testing rest calls, it has been added because AssertJ does not support yet. Deprecated in favor ofRestResultAssert
RestResultAssert
- fluent assertion for testing rest calls, it has been added because AssertJ does not support yet.RestTestUtils
- generates entities with mock data, creates page objets and converts object to json strings
Examples:
// mockito
verify(mock, times(1)).method();
verify(mock, times(2)).method();
doNothing().mock(mock).method(any(Class.class));
//bv mockito
verify(mock, once()).method();
verify(mock, twice()).method();
doNothing().mock(mock).method(anyClass());
//spring rest assertion
ResultAction result = mvc.perform(get("/test"));
result.andExpect( status().isOk() )
.andExpect( jsonPath( "$.value" ).value(expectedValue))
.andExpect( jsonPath("$.array", hasSize(size)));
//bv assertions
assertThat(result)
.isOk()
.extractDto(ActualDto.class)
.isEqualTo(expectedDto);
//rest test utils
RestTestUtils.generateTestEntity(Doctor.class);
RestTestUtils.generateTestEntityList(Doctor.class, 5)
RestTestUtils.pageOf(doctors)
RestTestUtils.asBodyJson(doctor)