Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GoikLectures
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Goik Martin
GoikLectures
Commits
ff1e1d3e
Commit
ff1e1d3e
authored
8 years ago
by
Goik Martin
Browse files
Options
Downloads
Patches
Plain Diff
Junit testing println(...) output
parent
696c403f
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Doc/Sd1/appendix.xml
+65
-0
65 additions, 0 deletions
Doc/Sd1/appendix.xml
with
65 additions
and
0 deletions
Doc/Sd1/appendix.xml
+
65
−
0
View file @
ff1e1d3e
...
@@ -478,6 +478,71 @@ java -jar ~/.m2/repository/de/hdm-stuttgart/mi/sd1/grep/0.9/grep-0.9.jar $*
...
@@ -478,6 +478,71 @@ java -jar ~/.m2/repository/de/hdm-stuttgart/mi/sd1/grep/0.9/grep-0.9.jar $*
<programlisting
language=
"none"
>
>
cat Testdata/input.txt | ./bin/mygrep red
<programlisting
language=
"none"
>
>
cat Testdata/input.txt | ./bin/mygrep red
The red cross acts worldwide
</programlisting>
The red cross acts worldwide
</programlisting>
</listitem>
</listitem>
<listitem>
<para>
Testing requires capturing of output being generated by e.g.
<methodname
xlink:href=
"https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#out"
>
System.out
</methodname><code>
.
</code><methodname
xlink:href=
"https://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html#println-java.lang.String-"
>
println(...)
</methodname>
calls. Consider the following code writing the string
<quote>
Hello
World!
</quote>
to standard output:
</para>
<programlisting
language=
"java"
>
public class App {
/**
* @param args Unused
*/
public static void main( String[] args ) {
System.out.print( "Hello World!" );
}
}
</programlisting>
<para>
We want to set up a
<productname>
Junit
</productname>
test
which captures the output to compare it with the expected string
value
<code>
"Hello World!"
</code>
. Following
<uri
xlink:href=
"http://stackoverflow.com/questions/1119385/junit-test-for-system-out-println"
>
http://stackoverflow.com/questions/1119385/junit-test-for-system-out-println
</uri>
we redefine the standard output stream by an private instance of
<classname
xlink:href=
"https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html"
>
java.io.ByteArrayOutputStream
</classname>
.
Due to
<productname>
Junit
</productname>
's @Before and @After
annotations this instance replaces
<classname
xlink:href=
"https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#out"
>
System.out
</classname>
during our tests:
</para>
<programlisting
language=
"java"
>
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AppTest {
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
@Before
public void setUpStreams() {
System.setOut(new PrintStream(outContent));
}
@After
public void cleanUpStreams() {
System.setOut(null);
outContent.reset();
}
/**
* Test method accessing output generated by System.out.println(...) calls.
*/
@Test
public void testApp() {
App.main(new String[]{}); // Calling main() method printing "Hello World!"
Assert.assertEquals("Hello World!", outContent.toString());
}
}
</programlisting>
</listitem>
</orderedlist>
</orderedlist>
</tip>
</tip>
</section>
</section>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment