Here is my code and I want to know what's the best way to test using Mockito as I am creating couple of objects using new keyword. can anyone guide me ?
public static PDDocument generatePDF(final String reportString, final String requestId) throws IOException {
final PDDocument document = new PDDocument();
final byte[] byteStr = reportString.getBytes(StandardCharsets.UTF_8);
final String str = new String(byteStr,
StandardCharsets.UTF_8);
final BufferedReader reader = new BufferedReader(new StringReader(str));
try {
// PDF box ceremony
final TextToPDF textToPdf = new TextToPDF();
textToPdf.setFont(PDType1Font.COURIER);
textToPdf.setFontSize(10);
textToPdf.createPDFFromText(document, reader);
reader.close();
} catch (final IOException ioException) {
LOGGER.error("IO Exception while generating PDF for request id " + requestId, ioException.getMessage());
throw ioException;
} catch (final Exception e) {
LOGGER.error("Exception while generating PDF for request id " + requestId, e.getMessage());
throw e;
} finally {
reader.close();
}
return document;
}
Mockito is intended to mock the collaborators of the class/method you want to test. Note that it should be used only to mock the types you own.
In this case, you don't actually need Mockito.
An example, similar to yours, in which you could use Mockito, is this:
class PDFGenerator {
private ITextToPdf textToPdf; // This is an hypotetical interface provided by you, for example used as a wrapper to easily change the underling framework
public void setTextToPdf(ITextToPdf textToPdf) {
this.textToPdf = textToPdf;
}
public static PDDocument generatePDF(final String reportString, final String requestId) throws IOException {
final byte[] byteStr = reportString.getBytes(StandardCharsets.UTF_8);
final String str = new String(byteStr,
StandardCharsets.UTF_8);
final BufferedReader reader = new BufferedReader(new StringReader(str));
try {
IDocument document = textToPdf.createPDFFromText(reader);
reader.close();
return document;
} catch (final IOException ioException) {
LOGGER.error("IO Exception while generating PDF for request id " + requestId, ioException.getMessage());
throw ioException;
} catch (final Exception e) {
LOGGER.error("Exception while generating PDF for request id " + requestId, e.getMessage());
throw e;
} finally {
reader.close();
}
}
}
In this case, the test would be:
#Test
public void testGeneratePdf() throws Exception {
ITextToPdf textToPdfMock Mockito.mock(ITextToPdf.class);
PDFGenerator pdfGenerator = new PDFGenerator();
pdfGenerator.setTextToPdf(textToPdfMock);
Mockito.when(textToPdfMock.createPDFFromText(Mockito.any())).thenReturn(something);
IDocument generatedDocument = pdfGenerator.generatePDF(createReportString(), "TestId");
Mockito.verify(textToPdfMock, Mockito.times(1)).createPDFFromText(Mockito.any());
Mockito.verifyNoMoreInteractions(textToPdfMock);
// Do also some standard junit asserts on the generatedDocument
}
Related
I am doing testing on the following piece of code, and i am having trouble getting this method to throw an IOException so i can get 100% coverage.
I have tried to mock the CharArrayReader, StringWriter classes but to no avail.
Would appreciate any help!
Class to test
public static final String getValue(String content) {
if (content == null) return null;
CharArrayReader reader = new CharArrayReader(content.toCharArray());
StringWriter writer = new StringWriter();
try {
int c;
while ((c = reader.read()) != -1) {
if (isChinese((char)c)) {
writer.write(c);
} else {
if ( (char)c > 0x20 && (char)c < 0x7f ) {
writer.write(c);
} else {
writer.write(' ');
}
}
}
} catch (IOException e) {
return null;
} finally {
reader.close();
}
return writer.toString();
}
My Attempts
#Test
public void getValue_Exception() throws IOException {
String content = "asd";
char[] chara = null;
CharArrayReader reader = mock(CharArrayReader.class);
when(content.toCharArray()).thenReturn(chara);
when(reader.read()).thenThrow(IOException.class);
StringWriter writer = mock(StringWriter.class);
doThrow(IOException.class).when(writer).write(anyInt());
spyController.getValue(content);
}
While reading inside the CharArrayReader class, the IOException is thrown when the char[] buff parameter in the constructor is null.
private void ensureOpen() throws IOException {
if (buf == null)
throw new IOException("Stream closed");
}
One approach (even if it is a bad idea to mock the String class) is to mock the call of the method toCharArray() from the String class to return a null value.
The only scenario, in which CharArrayReader::read throws IOException is when the stream is closed. In your example it seems rather impossible.
Nevertheless, if you really want to get that 100% coverage or just make sure your class behaves in case of unexpected, my advise would be to create a factory for your reader e.g.:
class ReaderFactory {
Reader create(String content) {
return new CharArrayReader(content.toCharArray());
}
}
With this class in place, you could use it in your code and mock the create method. This way you would have control over the instance of Reader being used in your test case.
I am using mockito-junit to test a piece of my code. As progressing I found out that there was an interface implemented in the main file which I was testing, when the test was running I found that the line where interface method is called get's covered but the real method doesn't get's covered.
This the code for the main file:
public class ExtractCurrencyDataTask {
private static final Logger LOGGER = LoggerFactory.getLogger(ExtractCurrencyDataTask.class);
#Autowired
private ExtractCurrencyService extractCurrencyService;
public void writeCurrencyListToFile(List<Currency> currencyList) {
if (currencyList != null && !currencyList.isEmpty()) {
String dir = "A path";
String fileName = "A filename";
String writeToFile = dir + "/" + fileName + ".writing";
String renameFile = dir + "/" + fileName + ".dat";
BufferedWriter writer = null;
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(writeToFile);
writer = new BufferedWriter(fileWriter);
extractCurrencyService.extractCurrencyList(currencyList, writer);
} catch (Exception e) {
throw new RuntimeException("Error writing Currency codes", e);
} finally {
if (writer != null) {
try {
writer.close();
fileWriter.close();
} catch (IOException e) {
LOGGER.info("Exception occured while closing the file writer", e);
}
moveFile(writeToFile, renameFile);
}
}
}
}
private void moveFile(String writeToFile, String renameFile) {
try {
FileUtils.moveFile(FileUtils.getFile(writeToFile), FileUtils.getFile(renameFile));
} catch (IOException e) {
LOGGER.info("Exception occured while moving file from writing to dat", e);
}
}
Here extractCurrencyService is the interface which I have mentioned.
The interface:
public interface ExtractCurrencyService {
public void extractCurrencyList(List<Currency> currency, Writer writer);
}
This the method definition which is done another file which implements same interface Filename:ExtractCurrencyServiceImpl.java
public class ExtractCurrencyServiceImpl implements ExtractCurrencyService {
private static final String SEP = "|";
private static final String NEWLINE = "\n";
#Override
public void extractCurrencyList(List<Currency> currencyList, Writer writer) {
if (currencyList != null) {
currencyList.forEach(currency -> {
String code = currency.getCode();
String name = currency.getName() == null ? "" : currency.getName();
Long noOfDecimals = currency.getNumberOfDecimals();
RoundingMethodValue roundingMethod = currency.getRoundingMethod();
boolean isDealCurrency = currency.isDealCurrency();
String description = currency.getDescription() == null ? "" : currency.getDescription();
try {
writer.write(createCurrencyDataLine(code,
name,
noOfDecimals,
roundingMethod,
isDealCurrency,
description));
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
}
private String createCurrencyDataLine(String code,
String name,
Long noOfDecimals,
RoundingMethodValue roundingMethod,
boolean isdealCurrency,
String description) {
return code + SEP + name + SEP + noOfDecimals.toString() + SEP + roundingMethod.toString() + SEP
+ isdealCurrency + SEP + description + NEWLINE;
}
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> map = new ConcurrentHashMap<>();
return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
}
This is the test file:
#RunWith(MockitoJUnitRunner.class)
public class ExtractCurrencyDataTaskTest {
#Mock
private Currency mockCurrency;
#Mock
private ExtractCurrencyService mockExtractCurrencyService;
#Mock
private BufferedWriter mockBufferWriter;
#Mock
private Bean mockBean;
#InjectMocks
private ExtractCurrencyDataTask extractCurrencyDataTask;
#Test
public void writeCurrencyListToFileTest() {
List<Currency> currencyList = new ArrayList();
when(mockCurrency.getCode()).thenReturn("USD");
when(mockCurrency.getNumberOfDecimals()).thenReturn((long) 2);
when(mockCurrency.getRoundingMethod()).thenReturn(enum value);
when(mockCurrency.isDealCurrency()).thenReturn(true);
when(mockCurrency.getName()).thenReturn("US Dollars");
when(mockCurrency.getDescription()).thenReturn("Currency Description");
currencyList.add(mockCurrency);
extractCurrencyDataTask.writeCurrencyListToFile(currencyList);
}
}
This the configuration for Autowired bean
#Bean
public ExtractCurrencyService extractCurrencyService() {
return new ExtractCurrencyServiceImpl();
}
As you can see the real output of this process is a file will be created in a path mentioned with some data. Here in this test I am mocking the data and passing it to main file. Main file is the created file in respective path but there is no data in the file.
The data writing part is done by the interface method. This is the part where I need help.
Thanks in advance....
You are injecting a mock of ExtractCurrencyService into your tested class. So the test is running with this mock instance instead of ExtractCurrencyServiceImpl. The current behaviour is that your ExtractCurrencyDataTasktested class is calling to extractCurrencyService#extractCurrencyList, but this extractCurrencyService is a mock, not your real implementation, so the call is done but it does nothing.
If you want to unit test ExtractCurrencyDataTask then thats ok, but maybe you should assert the call to extractCurrencyService#extractCurrencyList is done in the way you expect.
If you want to unit test ExtractCurrencyServiceImpl then create a unit test for this class.
If you want to test the interaction between these two classes then create an integration test where ExtractCurrencyDataTask has injected a real instance of ExtractCurrencyServiceImpl, not a mock.
I got a problem when files were download , I can't get any actions or events when clicking any links , butttons and menues after download process was done.
Below is my codes for excel file download button ...
Button btnDownloadExcel = new Button("Excel Download");
btnDownloadExcel.addStyleName("downloadButton");
btnDownloadExcel.addClickListener(new ClickListener() {
#Override
public void buttonClick(final ClickEvent event) {
StringBuilder url = new StringBuilder("/myproject/filedownload.html?category=excel");
url.append("&seq=" + 111);
getUI().getPage().open(url.toString(), "_self");
}
});
Below is servlet for handle excel file download request (I used JExcel API for excel file)
#WebServlet(value = "/filedownload.html")
public class DownloadServletController extends HttpServlet {
private final Logger log = LoggerFactory.getLogger(DownloadServletController.class);
protected final void doGet(final HttpServletRequest request,
final HttpServletResponse response) throws ServletException, IOException {
String category = request.getParameter("category");
long seq = request.getParameter("seq") == null ? -1L : Long.parseLong(request.getParameter("seq"));
byte[] stream = null;
if (category.equals("excel")) {
try {
stream = getSampleExcelStream(seq);
}
catch (BusinessException e) {
log.error("Generating streams for " + category + " got Error !" + e);
}
ExcelSupport.createExcel("Test", seq, stream, response);
}
}
private byte[] getSampleExcelStream(final long seq) throws BusinessException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
String staticDir = System.getProperty("staticDir");
String templateDir = staticDir + "/templates/sample_excel_template.xls";
WorkbookSettings wsWrite = new WorkbookSettings();
wsWrite.setEncoding("UTF-8");
wsWrite.setAutoFilterDisabled(false);
WritableWorkbook workBook = Workbook.createWorkbook(baos, Workbook.getWorkbook(new File(templateDir)),
wsWrite);
workBook.write();
baos.close();
workBook.close();
}
catch (BiffException e) {
throw new BusinessException("Excel file Creating Error!");
}
catch (WriteException e) {
throw new BusinessException("Error ! writing excel file process has occured!");
}
catch (FileNotFoundException e) {
throw new BusinessException("FileNotFoundException, when getting stream for excel", e);
}
catch (IOException e) {
throw new BusinessException("IOException, when getting stream for excel", e);
}
return baos.toByteArray();
}
}
ExcelSupport.java is below
public final class ExcelSupport {
private ExcelSupport() {
}
private static final Logger LOGGER = LoggerFactory.getLogger(ExcelSupport.class);
public static void createExcel(final String fileNamePrefix, final long seq,
final byte[] stream, final HttpServletResponse response) {
StringBuffer fileName = new StringBuffer();
fileName.append(fileNamePrefix + "_");
if (seq > -1) {
fileName.append("(uid-" + seq + ")_");
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
fileName.append(sdf.format(new Date()));
fileName.append(".xls");
StringBuffer sbContentDispValue = new StringBuffer();
sbContentDispValue.append("inline");
sbContentDispValue.append("; filename=");
sbContentDispValue.append(fileName);
response.setContentType("application/msexcel");
response.addHeader("Cache-Control", "max-age=30");
response.addHeader("Content-disposition", sbContentDispValue.toString());
response.setContentLength(stream.length);
try {
ServletOutputStream osStream = response.getOutputStream();
osStream.write(stream);
osStream.flush();
osStream.close();
}
catch (IOException e) {
LOGGER.error("Creating Excel for " + fileName + " got Error !" + e);
}
}
}
Can somebody correct me what I am wrong ? Download process was fine , nothing error and I got excel file successfully. But I have no idea why browser was freeze. I can't see any error logs or messages in IDE console and browser's console. Thanks for reading my question !
PS : I am sure this codes work fine and did not freeze on other GWT projects.
Now I found the problem . I used Network console of Firefox 31 and here is screen-shoot for before download and here is after download. I notice that I lost all web datas because the replacing url by getUI().getPage().open(url.toString(), "_self");
So , if I use others instead of _self , everythings were fine but browsers were block popups. I can't tell the users to enable popups of their browsers . So , finally I use Link component as below ..
Link linkDownloadExcel = new Link("Excel Download", new ExternalResource(
"/myproject/filedownload.html?category=excel&seq=" + 111), "_blank", -1, -1, BorderStyle.DEFAULT);
linkDownloadExcel.addStyleName("downloadButton");
linkDownloadExcel.setIcon(new ExternalResource("/myproject/images/excel-icon.png"));
hlButtonLayout.addComponent(linkDownloadExcel);
Does anyone know where to find a little how to on using dbpedia spotlight in java or scala? Or could anyone explain how it's done? I can't find any information on this...
The DBpedia Spotlight wiki pages would be a good place to start.
And I believe the installation page has listed the most popular ways (using a jar, or set up a web service) to use the application.
It includes instructions on using the Java/Scala API with your own installation, or calling the Web Service.
There are some additional data needed to be downloaded to run your own server for full service, good time to make a coffee for yourself.
you need download dbpedia spotlight (jar file) after that u can use next two classes ( author pablomendes ) i only make some change .
public class db extends AnnotationClient {
//private final static String API_URL = "http://jodaiber.dyndns.org:2222/";
private static String API_URL = "http://spotlight.dbpedia.org:80/";
private static double CONFIDENCE = 0.0;
private static int SUPPORT = 0;
private static String powered_by ="non";
private static String spotter ="CoOccurrenceBasedSelector";//"LingPipeSpotter"=Annotate all spots
//AtLeastOneNounSelector"=No verbs and adjs.
//"CoOccurrenceBasedSelector" =No 'common words'
//"NESpotter"=Only Per.,Org.,Loc.
private static String disambiguator ="Default";//Default ;Occurrences=Occurrence-centric;Document=Document-centric
private static String showScores ="yes";
#SuppressWarnings("static-access")
public void configiration(double CONFIDENCE,int SUPPORT,
String powered_by,String spotter,String disambiguator,String showScores){
this.CONFIDENCE=CONFIDENCE;
this.SUPPORT=SUPPORT;
this.powered_by=powered_by;
this.spotter=spotter;
this.disambiguator=disambiguator;
this.showScores=showScores;
}
public List<DBpediaResource> extract(Text text) throws AnnotationException {
LOG.info("Querying API.");
String spotlightResponse;
try {
String Query=API_URL + "rest/annotate/?" +
"confidence=" + CONFIDENCE
+ "&support=" + SUPPORT
+ "&spotter=" + spotter
+ "&disambiguator=" + disambiguator
+ "&showScores=" + showScores
+ "&powered_by=" + powered_by
+ "&text=" + URLEncoder.encode(text.text(), "utf-8");
LOG.info(Query);
GetMethod getMethod = new GetMethod(Query);
getMethod.addRequestHeader(new Header("Accept", "application/json"));
spotlightResponse = request(getMethod);
} catch (UnsupportedEncodingException e) {
throw new AnnotationException("Could not encode text.", e);
}
assert spotlightResponse != null;
JSONObject resultJSON = null;
JSONArray entities = null;
try {
resultJSON = new JSONObject(spotlightResponse);
entities = resultJSON.getJSONArray("Resources");
} catch (JSONException e) {
//throw new AnnotationException("Received invalid response from DBpedia Spotlight API.");
}
LinkedList<DBpediaResource> resources = new LinkedList<DBpediaResource>();
if(entities!=null)
for(int i = 0; i < entities.length(); i++) {
try {
JSONObject entity = entities.getJSONObject(i);
resources.add(
new DBpediaResource(entity.getString("#URI"),
Integer.parseInt(entity.getString("#support"))));
} catch (JSONException e) {
LOG.error("JSON exception "+e);
}
}
return resources;
}
}
second class
/**
* #author pablomendes
*/
public abstract class AnnotationClient {
public Logger LOG = Logger.getLogger(this.getClass());
private List<String> RES = new ArrayList<String>();
// Create an instance of HttpClient.
private static HttpClient client = new HttpClient();
public List<String> getResu(){
return RES;
}
public String request(HttpMethod method) throws AnnotationException {
String response = null;
// Provide custom retry handler is necessary
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
try {
// Execute the method.
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
LOG.error("Method failed: " + method.getStatusLine());
}
// Read the response body.
byte[] responseBody = method.getResponseBody(); //TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
response = new String(responseBody);
} catch (HttpException e) {
LOG.error("Fatal protocol violation: " + e.getMessage());
throw new AnnotationException("Protocol error executing HTTP request.",e);
} catch (IOException e) {
LOG.error("Fatal transport error: " + e.getMessage());
LOG.error(method.getQueryString());
throw new AnnotationException("Transport error executing HTTP request.",e);
} finally {
// Release the connection.
method.releaseConnection();
}
return response;
}
protected static String readFileAsString(String filePath) throws java.io.IOException{
return readFileAsString(new File(filePath));
}
protected static String readFileAsString(File file) throws IOException {
byte[] buffer = new byte[(int) file.length()];
#SuppressWarnings("resource")
BufferedInputStream f = new BufferedInputStream(new FileInputStream(file));
f.read(buffer);
return new String(buffer);
}
static abstract class LineParser {
public abstract String parse(String s) throws ParseException;
static class ManualDatasetLineParser extends LineParser {
public String parse(String s) throws ParseException {
return s.trim();
}
}
static class OccTSVLineParser extends LineParser {
public String parse(String s) throws ParseException {
String result = s;
try {
result = s.trim().split("\t")[3];
} catch (ArrayIndexOutOfBoundsException e) {
throw new ParseException(e.getMessage(), 3);
}
return result;
}
}
}
public void saveExtractedEntitiesSet(String Question, LineParser parser, int restartFrom) throws Exception {
String text = Question;
int i=0;
//int correct =0 ; int error = 0;int sum = 0;
for (String snippet: text.split("\n")) {
String s = parser.parse(snippet);
if (s!= null && !s.equals("")) {
i++;
if (i<restartFrom) continue;
List<DBpediaResource> entities = new ArrayList<DBpediaResource>();
try {
entities = extract(new Text(snippet.replaceAll("\\s+"," ")));
System.out.println(entities.get(0).getFullUri());
} catch (AnnotationException e) {
// error++;
LOG.error(e);
e.printStackTrace();
}
for (DBpediaResource e: entities) {
RES.add(e.uri());
}
}
}
}
public abstract List<DBpediaResource> extract(Text text) throws AnnotationException;
public void evaluate(String Question) throws Exception {
evaluateManual(Question,0);
}
public void evaluateManual(String Question, int restartFrom) throws Exception {
saveExtractedEntitiesSet(Question,new LineParser.ManualDatasetLineParser(), restartFrom);
}
}
main()
public static void main(String[] args) throws Exception {
String Question ="Is the Amazon river longer than the Nile River?";
db c = new db ();
c.configiration(0.0, 0, "non", "CoOccurrenceBasedSelector", "Default", "yes");
System.out.println("resource : "+c.getResu());
}
I just add one little fix for your answer.
Your code is running, if you add the evaluate method call:
public static void main(String[] args) throws Exception {
String question = "Is the Amazon river longer than the Nile River?";
db c = new db ();
c.configiration(0.0, 0, "non", "CoOccurrenceBasedSelector", "Default", "yes");
c.evaluate(question);
System.out.println("resource : "+c.getResu());
}
Lamine
In the request method of the second class (AnnotationClient) in Adel's answer, the author Pablo Mendes hasn't finished
TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
which is an annoying warning that needs to be removed by replacing
byte[] responseBody = method.getResponseBody(); //TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
response = new String(responseBody);
with
Reader in = new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8");
StringWriter writer = new StringWriter();
org.apache.commons.io.IOUtils.copy(in, writer);
response = writer.toString();
I have got this piece of code (I didn't write, just maintaining):
public class MyMultipartResolver extends CommonsMultipartResolver{
public List parseEmptyRequest(HttpServletRequest request) throws IOException, FileUploadException {
String contentType = request.getHeader(CONTENT_TYPE);
int boundaryIndex = contentType.indexOf("boundary=");
InputStream input = request.getInputStream();
byte[] boundary = contentType.substring(boundaryIndex + 9).getBytes();
MultipartStream multi = new MultipartStream(input, boundary);
multi.setHeaderEncoding(getHeaderEncoding());
ArrayList items = new ArrayList();
boolean nextPart = multi.skipPreamble();
while (nextPart) {
Map headers = parseHeaders(multi.readHeaders());
// String fieldName = getFieldName(headers);
String subContentType = getHeader(headers, CONTENT_TYPE);
if (subContentType == null) {
FileItem item = createItem(headers, true);
OutputStream os = item.getOutputStream();
try {
multi.readBodyData(os);
} finally {
os.close();
}
items.add(item);
} else {
multi.discardBodyData();
}
nextPart = multi.readBoundary();
}
return items;
}
}
I am using commons-fileupload.jar version 1.2.1 and obviously the code is using some deprecated methods...
Anyway, while trying to use this code to upload a very large file (780 MB) I get this:
org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
at org.apache.commons.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:983)
at org.apache.commons.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:887)
at java.io.InputStream.read(InputStream.java:89)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:94)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:64)
at org.apache.commons.fileupload.MultipartStream.readBodyData(MultipartStream.java:593)
at org.apache.commons.fileupload.MultipartStream.discardBodyData(MultipartStream.java:619)
that is thrown from 'multi.discardBodyData();' line.
My question:
How can I avoid this error and be able to be able to succeed collecting the FileItems?
catch
(org.apache.commons.fileupload.MultipartStream.MalformedStreamException e)
{
e.printStackTrace();
return ERROR;
}
Catch the exception and handle it via ..either InputStream or Return Error use it in struts action tag