Andrew Holovko
@andrewholovko
Blog of a self-taught programmer writing about the experience, tips and other stuff
158 posts

Optimizing repository.saveAll() JPA Hibernate

Problem. Invoking repository.saveAll() has to insert into DB by batches, but it doesn't.

Solver: pangram problem

A pangram is a string that contains every letter of the alphabet. Given a sentence determine whether it is a pangram in the English alphabet. Ignore case. Return either pangram or not pangram as appropriate.

Concurrent Bank Transaction System in Java

Below is an example of a concurrent bank transaction system in Java. In this example, we model a bank account with methods for deposit, withdrawal, and a transfer operation that involves two accounts. We use a ReentrantLock in each account to ensure that modifications to the account balance are thread-safe, and we take care to avoid deadlocks when transferring funds between two accounts.

How to integrate kafka-prometheus-exporter and NestJs prometheus module

Monitoring applications is crucial for understanding their performance, availability, and health. In this article, we’ll walk through setting up monitoring for a NestJS application using Prometheus.

M04 Q44 When do you want use @DataJpaTest for? What does it auto-configure?

You want to use @DataJpaTest annotation whenever writing an Integration Test for JPA related components of your application like Entities or Repositories.

M04 Q43 What are the differences between @MockBean and @Mock?

@Mock annotation comes from Mockito Framework which allows for easy Mock creation. This annotation is used by MockitoJUnitRunner, each field annotated with it will have Mock for specified class created. This annotation does not inject mocks into tested class on itself, to use injection you need to have target class annotated with @InjectMocks annotation.

M04 Q42 When do you want to use @WebMvcTest? What does it auto-configure?

You should use @WebMvcTest annotation when you want to write Integration Test that is focused on web layer of your application. @WebMvcTest approach will create ApplicationContext that contains only web components and omits any other components that are not part of web layer. Other components, if required for the test, can be mocked with usage of @MockBean annotation or delivered by @Configuration annotated class imported with usage of @Import annotation.

M04 Q41 How do you perform integration testing with @SpringBootTest for a web application?

Integration Test by definition, should check interactions between few components of the system (at least two real, not-mocked components) to check if those components are delivering expected functionalities when working together. In each case when writing Integration Test you should decide how many components should interact in the test for it to be meaningful. Usually, you should decide on smallest possible amount of components that are enough to test specific functionality. Components that are not meaningful can be omitted, or mocked with usage of @MockBean annotation.