Java experience sheet

Read files > 1 GB lazily

This reads big files (>200 MBs) sequentially and without loading the whole File in memory. This way we’re able to read text files on the Gigabyte level. This example was done reading from a remote SFTP server.

final ChannelSftp sftpClient = this.connect();
final InputStream is = sftpClient.get(file);
final InputStreamReader isReader
      = new InputStreamReader(is);  

try (final BufferedReader bffReader  
      = new BufferedReader(isReader)) {
} catch(final IOException ex) {
  log.error("bla", ex);

Operate a Stream in batches

(This solution uses Google Guava)

I have a really big List<Object> with f.e. 600.000 entries and I’d like to be able to operate them in batches of n size.

public void handle() {
  final int batchSize = 100; // n
  final List<MyEntity> list = this.dao.findAll();
  Iterators.partition(list.iterator(), batchSize)

private void consumer(final List<MyEntity> batch) {
  // this will contain 100 Entities
  // do x


Iterate, operate and delete from a single List

This has the benefit we don’t need to create another List to contain the result of our operation. It comes really handy when we’ve to operate big Lists of items (+500.000) and we’re already near the limit of our VM.

 * @param originalItems
 *          huge list
 * @param filteredItems
 *          map to filter them into
private void filterAndRemove(
      final List<TYPE> originalItems,
      final Map<String, List<TYPE>> filteredItems)
  final ListIterator<TYPE> origIterator =
  for (int idx = 0; origIterator.hasNext(); idx++)
    final TYPE item =;
    final String key = this.buildKey(item);
    filteredItems.put(key, item);
    this.clearMemoryEvery(idx, 1000);

Convert checked into unchecked exception

(Test example in private Repo)

Use Unchecked.consumer()

private OrderRepo repo;

public File exportFile(String fileName) {
  File file = new File("export/" + fileName);
  try (Writer writer = new FileWriter(file)) {
    .map(o -> o.getId() + ";"
    	+ o.getCreationDate())


Conference (18:45)


Obtain last day of a month

(This uses Java8 Time API)

final LocalDate invoiceDate =  
final int month = invoiceDate.getMonthValue();
final int year = invoiceDate.getYear();
final YearMonth yearMonth =   
		YearMonth.of(year, month);
final int lastDayOfMonth =   


The key is to give a fixed clock to LocalDate (Java 8).
First of all we prepare the real clock to be injected.

// This goes into our Spring's config class.
public Clock clock() {
  return Clock.systemDefaultZone();

We inject and use it

public class Whatever {

  private Clock clock;

  public LocalDate obtainDate() {


And inside the test class, we can just mock it

public void testLocalDate() {
  // Given
  final String date = "2019-07-03";
  final DateTimeFormatter formatter =
  final LocalDateTime dateTime =
    LocalDateTime.parse(date, formatter);

  final Clock fixedClock = Clock.fixed(  

  // the clock is now ready to be injected and / or used

  // When
  // Then



Inject SpringContext into IT (TestNG)

To inject Spring’s context into an Integration-Test. Instead of @RunWith(…) [JUnit]

@ContextConfiguration(classes = { SpringConfig.class })
public class MyAutowiredTestNGTest extends AbstractTestNGSpringContextTests {

	private Object whatever;



Inject SpringContext into IT (JUnit4)

A good recommendation is to have an AppTestConfig.class for your tests, independent of the real bean.

@ContextConfiguration(classes= { AppTestConfig.class })
public class MyAutowiredJUnit4Test  {

    // here may you set your own class to inject

    private ApplicationContext context;



MergedAnnotations$SearchStrategy error

When trying to @Autowire a JUnit4 test and it gives this error back

java.lang.NoClassDefFoundError: org.springframework.core.annotation.MergedAnnotations$SearchStrategy

Check we have uniform Spring versions at POM, as they cannot differ between test and normal dependencies.


(This uses BDDCatchException, Mockito and AssertJ)

Mock an exception, which will be thrown and caught

// Given

// When

// Then

Test an exception is thrown:

// Given
CrudService service = new CrudService() {};
String userName = "admin";

// When

// Then

Test no exception is thrown:

// Then  

Inject mock into ‘new’ operator

// Real class  
protected JSch createStubInstance() {
	neturn new JSch();

// Test class  
public void testWhatever() {  
  JSch jschMock = Mockito.mock(JSch.class);  



Link javadoc to a method of the same class

* This method does the same as
* {@link #escape(Parameters) escape}
* but with lists


Clean Dirty context

When we use Spring’s DI in an integration test, the context may be dirty between every test and must be cleaned. This is done with @DirtiesContext tag.

@ContextConfiguration(classes =  
	{ CoreConfig.class })  
@DirtiesContext(classMode = ClassMode.BEFORE_EACH_TEST_METHOD)  
public class ASCIISystemIT { ... }  


Maven builds, but IntelliJ doesn’t find imports

  1. From IntelliJ execute a mvn clean install
  2. Execute maven goal mvn dependency:purge-local-repository
  3. Build
  4. Rebuild project