Created
July 1, 2011 08:59
-
-
Save mgenov/1058129 to your computer and use it in GitHub Desktop.
PrintArrayTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PrintArrayTest { | |
interface Display { | |
void print(String message); | |
} | |
public void printArray(int[] array, Display display) { | |
int index = 0; | |
for (int item : array) { | |
display.print("array[" + index + "]=" + item + ","); | |
index++; | |
} | |
} | |
class InMemoryDisplay implements Display { | |
private StringBuilder printedMessages = new StringBuilder(); | |
public void print(String message) { | |
printedMessages.append(message); | |
} | |
private void assertHasPrintedMessage(String expectedMessage) { | |
assertThat("the displayed message in the display was different from expected?",printedMessages.toString(), is(equalTo(expectedMessage))); | |
} | |
} | |
@Test | |
public void printArray() { | |
InMemoryDisplay display = new InMemoryDisplay(); | |
printArray(new int[] {1, 2}, display); | |
display.assertHasPrintedMessage("array[0]=1,array[1]=2,"); | |
} | |
} |
Author
mgenov
commented
Jul 5, 2011
via email
1- what is the use of the interface here ?
As you can see, the interface is used to fake (read more about
mock,fakes,stubs and etc) the real console output, so I can assert the
printed content. I used it to show you that my test is shorter then your
test. In fact, the same behavior could be achieved and by using a simple
PrintWriter (System.out is instance of PrintWriter).
For example
public void printArray(int[] array, PrintWriter writer) { ... }
So to test it, we could use something like:
ByteArrayOutputStream bout = new ByteArrayOutputStream();
PrintWriter writer = new PrintWriter(bout);
printArray(new int[] { 1, 2, 3} , writer);
// we need to call flush to be sure that all content is written to the
output stream
writer.flush();
String actual = new String(bout.toByteArray());
assertThat(actual,is(equalTo(expected));
As you can see, there is no additional interface and etc, but the problem
here is that if I want to test a scenario where the data cannot be printed
you have to do some magic around ByteArrayOutputStream to throw IOException,
where in my case with interface I can mock the interface using
jmock/easymock/mockito or any other mocking library to simulate the error
behavior.
2- why did you re-implement printArray this way ? i mean printArray PRINTS
(DISPLAYS ON THE SCREEN ), but this one is appending to StringBuilder object
created in InMemoryDisplay Class ?
I used an fake implementation named InMemoryDisplay, to simulate the real
console output writer, so I can assert the response. The problem with your
test is that the System.out is tight coupled with the definition in your
method. To fix that I extracted that dependency as a method parameter.
3- why the assertion method is part of InMemoryClass not part of printArray
Nothing obligates me to insert my assertions in the test method. I prefer
that way, cause I don't have to write a similar assert in all of my test
methods, i.e removing code duplication.
4- why you don't follow the naming convention of JUnit as it is mentioned in
the books ? or this is up to you since there is the @test annotation?
JUnit 3 requires that, but you are using JUnit 4 which requires @test
annotation only. I think that the word "test" in front of your test doesn't
gives you more information about what you are verifying. The most of the
books are junit 3 related, that's why the examples are starting with
"test".
5- Too may question, sorry :)
Yeah, too many for few lines of code :)
On Tue, Jul 5, 2011 at 1:58 AM, fireball183 < ***@***.***>wrote:
Here are some questions that i hope to get clear answer or direct links or
notes otherwise
1- what is the use of the interface here ?
2- why did you re-implement printArray this way ? i mean printArray PRINTS
(DISPLAYS ON THE SCREEN ), but this one is appending to StringBuilder object
created in InMemoryDisplay Class ?
3- why the assertion method is part of InMemoryClass not part of printArray
4- why you don't follow the naming convention of JUnit as it is mentioned
in the books ? or this is up to you since there is the @test annotation?
5- Too may question, sorry :)
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/1058129
##
The human knowledge belongs to the world.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment