[PJUG Javamail] can I do text/diff-based testing with JUnit?

Lyle T. Harris Lyle.Harris at Sun.COM
Thu Oct 23 17:25:58 EDT 2008


Actually, JUnit has assertArrayEquals method so reading the files
into arrays and testing them would be better since it would also
take line order into account.  I thought the Groovy presentation
implied that JUnit didn't have such an animal. 

I would suggest writing a simple method like this:

    String[] load(File file) {
       // Load the file and each element equals one line
    }

Then you could just use it in your test cases:

    assertArrayEquals(load(new File("Golden.txt")), load(new 
File("Ouput.txt")));


> How about leveraging some Groovy as put forward in the last presentation.
>
> List golden  = new File("Golden.txt").readLines();
> List current = new File("CurrentTest.txt").readLines();
>
> assertTrue(current.containsAll(golden));
> assertTrue(golden.containsAll(current));
>
> You could also just write an equivalent
>
>      List readLines(File file);
>
> method in Java yourself.
>
>
>
>
> Send Here wrote:
>> I recently started using JUnit and previously used a custom in-house 
>> testing environment which was heavily text-based.  The old 
>> environment is no longer available.  I'm wondering if there's an easy 
>> way to adapt JUnit to text-based/diff-based testing, or if I should 
>> use another testing framework entirely, or if I am just abusing JUnit 
>> and should find a way to use it how it expects to be used.
>>
>> By "text-based" I mean every testcase produces some kind of text 
>> output which can be saved into a file and diff'ed against a 
>> previously approved text output (aka golden reference).  The first 
>> time you create a testcase, you visually inspect the text and see if 
>> it is ok:
>>    This is the first line of text output
>>    I expect this result on the second line
>>    And this on the third line
>> If that's good, you copy the output to a golden reference area, and 
>> then the next time you run it if the current results have a diff then 
>> you know you have something to fix:
>>    > I didn't expect this on the second line
>>
>> I suppose I could read the text as an array of Strings and write a 
>> bunch of JUnit assert stmts for this instead, e.g.
>>    assertEquals(lines[0], "This is the first line of text output");
>>    assertEquals(lines[1], "I expect this result on the second line");
>>    assertEquals(lines[2], "And this on the third line");
>> ...but that will become very cumbersome very quickly when there is a 
>> lot of output, and also cumbersome when you change things 
>> intentionally and need to update the golden reference.
>>
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> Web Site - http://www.pjug.org/
>> Javamail mailing list
>> Javamail at pjug.org
>> http://www.pjug.org/mailman/listinfo/javamail
>>   
>
> _______________________________________________
> Web Site - http://www.pjug.org/
> Javamail mailing list
> Javamail at pjug.org
> http://www.pjug.org/mailman/listinfo/javamail
>   

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.pjug.org/pipermail/javamail/attachments/20081023/12489054/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: lyle_harris.vcf
Type: text/x-vcard
Size: 232 bytes
Desc: not available
Url : http://www.pjug.org/pipermail/javamail/attachments/20081023/12489054/attachment.vcf 


More information about the Javamail mailing list