Java Notes
My notes on Java and libraries associated with Java. Notes in the public.
Concepts
Streams
- Can create stream of any type of Collection (Collection, List, Set)
Stream<String> streamBuilder = Stream.<String>builder().add("a").add("b").add("c").build();
Stream<String> streamGenerated = Stream.generate(() -> "element").limit(10);
- stream pipeline
- source, intermediate operation(s) and a terminal operation.
reduce()
Libraries
Spring Boot
https://roadmap.sh/spring-boot
- Spring Events - Observer Pattern
- Spring Cloud Stream
JUnit
Notes:
- By default, When each test function is run a new instance of the test class is instantiated. This means mocks declared on the class are “reset” for each test.
class TestCase {
@Test
void testFunction() {
// Given
// When
// Then
assertTrue()
assumeFalse()
assertEquals()
assertArrayEquals()
assertNotNull()
assertNull()
}
}
AssertJ
https://assertj.github.io/doc/
class TestCase {
@Test
void testFunction() {
// Given
// When
// Then
assertThat(frodo)
.isNotEqualTo(sauron)
.isIn(fellowshipOfTheRing);
assertThat(frodo.getName())
.startsWith("Fro")
.endsWith("do")
.isEqualToIgnoringCase("frodo");
assertThat(fellowshipOfTheRing)
.hasSize(9)
.contains(frodo, sam)
.doesNotContain(sauron);
}
Mockito
@RunWith(SpringRunner.class)
public class MockBeanAnnotationIntegrationTest {
@MockBean
UserRepository mockRepository;
@Autowired
ApplicationContext context;
@Test
public void givenCountMethodMocked_WhenCountInvoked_ThenMockValueReturned() {
Mockito.when(mockRepository.count()).thenReturn(123L);
UserRepository userRepoFromContext = context.getBean(UserRepository.class);
long userCount = userRepoFromContext.count();
Assert.assertEquals(123L, userCount);
Mockito.verify(mockRepository).count();
}
}
wiremock