Monday, 2 January 2017

java - how to write junit test cases for void function which prints output to console

I've two functions addKeywords and search which have void return type and search function prints results to the console.
Here is the code




 void addKeywords(String video)throws IOException{


InputStreamReader ip = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ip);

Integer previousVal=0;

if(!videoKeyword.containsKey(video) && videoNames.contains(video)){

videoKeyword.put(video,new HashSet());
}
else if(!videoNames.contains(video)){
System.out.println("Video is not a part of lookup");
}

System.out.println("Enter keywords for video");
String keyword =br.readLine();

if(!keywordLength.containsKey(video))

keywordLength.put(video, 0);

if((keywordLength.get(video)+keyword.length())<500){
videoKeyword.get(video).add(keyword);
previousVal=keywordLength.get(video);
keywordLength.put(video, previousVal+keyword.length());
}
else{
System.out.println("Maximum length exceeded for video "+ video);
}

if(!kewordVideo.containsKey(keyword)){
kewordVideo.put(keyword,new HashSet());
}
kewordVideo.get(keyword).add(video);
}

void search(String searchKey){
for (Entry> entry : videoKeyword.entrySet()) {
for (String s : entry.getValue()) {
if (s.startsWith(searchKey)) {

System.out.println(searchKey+" is mapped to "+entry.getKey());
break;
}
}
}
}


I have written junit tests




 public class MyUnitTest extends CultureMachineAssignment {
CultureMachineAssignment testObj =new CultureMachineAssignment();
testObj.insertDataIntoSet();
testObj.addkeywords("video1");

@Test
public void testVideo() {
assertEquals("video1", testObj.search("abcd"));
}
}



I am getting following errors



The method assertEquals(Object, Object) in the type Assert is not applicable for the arguments
(String, void)




  • Syntax error on token ""video1"", delete
    this token


  • Syntax error on token(s), misplaced
    construct(s)



I am not sure if this is the correct way to write junit test cases for functions with void return type and which prints output to console.
Can someone please tell me correct code ?

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...