Kapil Nimje
Kapil Nimje

Reputation: 81

How do i verify string content using Mockito in Java

I am new to using Mockito test framework. I need to unit test one method which return the the string content. Also the same contents will be stored in one .js file (i.e. "8.js"). How do I verify the the string contents returned from the method is as expected as i want.

Please find the below code for generating the .js file:

public String generateJavaScriptContents(Project project)
   {

      try
      {
         // Creating projectId.js file
         FileUtils.mkdir(outputDir);
         fileOutputStream = new FileOutputStream(outputDir + project.getId() + ".js");
         streamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
         StringTemplateGroup templateGroup =
            new StringTemplateGroup("viTemplates", "/var/vi-xml/template/", DefaultTemplateLexer.class);
         stringTemplate = templateGroup.getInstanceOf("StandardJSTemplate");
         stringTemplate.setAttribute("projectIdVal", project.getId());
         stringTemplate.setAttribute("widthVal", project.getDimension().getWidth());
         stringTemplate.setAttribute("heightVal", project.getDimension().getHeight());
         stringTemplate.setAttribute("playerVersionVal", project.getPlayerType().getId());
         stringTemplate.setAttribute("finalTagPath", finalPathBuilder.toString());
         streamWriter.append(stringTemplate.toString());
         return stringTemplate.toString();
      }
      catch (Exception e)
      {
         logger.error("Exception occurred while generating Standard Tag Type Content", e);
         return "";
      }

   }

The output of above method writes the .js file and the contents of that file are looks something below:

var projectid = 8;
var playerwidth = 300;
var playerheight = 250;
var player_version = 1;
.....

I have written the testMethod() using mockito to test this, however i am able to write the .js file successfully using the test method, but how do I verify its contents?

Can anyone help me to sort out this problem?

Upvotes: 0

Views: 1038

Answers (2)

Justin Muller
Justin Muller

Reputation: 1303

As @ŁukaszBachman mentions, you can read the contents from the js file. There are a couple of things to consider when using this approach:

  1. The test will be slow, as you will have to wait for the js content to be written to the disk, read the content back from the disk and assert the content.
  2. The test could theoretically be flaky because the entire js content may not be written to the disk by the time the code reads from the file. (On that note, you should probably consider calling flush() and close() on your OutputStreamWriter, if you aren't already.)

Another approach is to mock your OutputStreamWriter and inject it into the method. This would allow you to write test code similar to the following:

OutputStreamWriter mockStreamWriter = mock(OutputStreamWriter.class);
generateJavaScriptContents(mockStreamWriter, project);
verify(mockStreamWriter).append("var projectid = 8;\nvar playerwidth = 300;...");

http://mockito.googlecode.com/svn/branches/1.5/javadoc/org/mockito/Mockito.html#verify%28T%29

Upvotes: 4

ŁukaszBachman
ŁukaszBachman

Reputation: 33735

If you persist this *.js file on file system then simply create util method which will read it's contents and then use some sort of assertEquals to compare it with your fixed data.

Here is code for reading file contents into String.

Upvotes: 0

Related Questions