DDMFormInstanceRecordXLSWriter implements DDMFormInstanceRecordWriter { @Override public DDMFormInstanceRecordWriterResponse write( DDMFormInstanceRecordWriterRequest ddmFormInstanceRecordWriterRequest) throws Exception { Map<String, String> ddmFormFieldsLabel = ddmFormInstanceRecordWriterRequest.getDDMFormFieldsLabel(); int rowIndex = 0; try (ByteArrayOutputStream byteArrayOutputStream = createByteArrayOutputStream(); Workbook workbook = createWorkbook()) { Sheet sheet = workbook.createSheet(); CellStyle headerCellStyle = createCellStyle( workbook, true, "Courier New", (short)14); createRow( rowIndex++, headerCellStyle, ddmFormFieldsLabel.values(), sheet); CellStyle rowCellStyle = createCellStyle( workbook, false, "Courier New", (short)12); List<Map<String, String>> ddmFormFieldsValueList = ddmFormInstanceRecordWriterRequest.getDDMFormFieldValues(); for (Map<String, String> ddmFormFieldsValue : ddmFormFieldsValueList) { createRow( rowIndex++, rowCellStyle, ddmFormFieldsValue.values(), sheet); } workbook.write(byteArrayOutputStream); DDMFormInstanceRecordWriterResponse.Builder builder = DDMFormInstanceRecordWriterResponse.Builder.newBuilder( byteArrayOutputStream.toByteArray()); return builder.build(); } }  @Override DDMFormInstanceRecordWriterResponse write(
			DDMFormInstanceRecordWriterRequest
				ddmFormInstanceRecordWriterRequest);  }
@Test public void testWrite() throws Exception { Map<String, String> ddmFormFieldsLabel = Collections.emptyMap(); List<Map<String, String>> ddmFormFieldValues = new ArrayList<Map<String, String>>() { { add( HashMapBuilder.put( "field1", "2" ).build()); add( HashMapBuilder.put( "field1", "1" ).build()); } }; DDMFormInstanceRecordWriterRequest.Builder builder = DDMFormInstanceRecordWriterRequest.Builder.newBuilder( ddmFormFieldsLabel, ddmFormFieldValues); DDMFormInstanceRecordWriterRequest ddmFormInstanceRecordWriterRequest = builder.build(); DDMFormInstanceRecordXLSWriter ddmFormInstanceRecordXLSWriter = mock( DDMFormInstanceRecordXLSWriter.class); ByteArrayOutputStream byteArrayOutputStream = mock( ByteArrayOutputStream.class); when( ddmFormInstanceRecordXLSWriter.createByteArrayOutputStream() ).thenReturn( byteArrayOutputStream ); when( byteArrayOutputStream.toByteArray() ).thenReturn( new byte[] {1, 2, 3} ); Workbook workbook = mock(Workbook.class); when( ddmFormInstanceRecordXLSWriter.createWorkbook() ).thenReturn( workbook ); Mockito.doNothing( ).when( workbook ).write( byteArrayOutputStream ); when( ddmFormInstanceRecordXLSWriter.write( ddmFormInstanceRecordWriterRequest) ).thenCallRealMethod(); DDMFormInstanceRecordWriterResponse ddmFormInstanceRecordWriterResponse = ddmFormInstanceRecordXLSWriter.write(builder.build()); Assert.assertArrayEquals( new byte[] {1, 2, 3}, ddmFormInstanceRecordWriterResponse.getContent()); InOrder inOrder = Mockito.inOrder( ddmFormInstanceRecordXLSWriter, workbook, byteArrayOutputStream); inOrder.verify( workbook, Mockito.times(1) ).createSheet(); inOrder.verify( ddmFormInstanceRecordXLSWriter, Mockito.times(1) ).createCellStyle( Matchers.any(Workbook.class), Matchers.anyBoolean(), Matchers.anyString(), Matchers.anyByte() ); inOrder.verify( ddmFormInstanceRecordXLSWriter, Mockito.times(1) ).createRow( Matchers.anyInt(), Matchers.any(CellStyle.class), Matchers.anyCollection(), Matchers.any(Sheet.class) ); inOrder.verify( ddmFormInstanceRecordXLSWriter, Mockito.times(1) ).createCellStyle( Matchers.any(Workbook.class), Matchers.anyBoolean(), Matchers.anyString(), Matchers.anyByte() ); inOrder.verify( ddmFormInstanceRecordXLSWriter, Mockito.times(2) ).createRow( Matchers.anyInt(), Matchers.any(CellStyle.class), Matchers.anyCollection(), Matchers.any(Sheet.class) ); inOrder.verify( workbook, Mockito.times(1) ).write( byteArrayOutputStream ); inOrder.verify( byteArrayOutputStream, Mockito.times(1) ).toByteArray(); }