Skip to content

Commit 793c42e

Browse files
committed
Refactor App and made AppTest properly test App.
* Update version of junit dependency in 'pom.xml' file (to 4.11) so that org.junit dependencies in AppTest could be imported.
1 parent b132e29 commit 793c42e

File tree

3 files changed

+48
-30
lines changed

3 files changed

+48
-30
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<dependency>
1212
<groupId>junit</groupId>
1313
<artifactId>junit</artifactId>
14-
<version>3.8.1</version>
14+
<version>4.11</version>
1515
<scope>test</scope>
1616
</dependency>
1717
</dependencies>

src/main/java/com/mycompany/app/App.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@
22

33
/**
44
* Hello world!
5-
*
65
*/
7-
public class App
6+
public class App
87
{
9-
public static void main( String[] args )
10-
{
11-
System.out.println( "Hello World!" );
8+
9+
private final String message = "Hello World!";
10+
11+
public App() {}
12+
13+
public static void main(String[] args) {
14+
System.out.println(new App().getMessage());
1215
}
16+
17+
private final String getMessage() {
18+
return message;
19+
}
20+
1321
}
Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
11
package com.mycompany.app;
22

3-
import junit.framework.Test;
4-
import junit.framework.TestCase;
5-
import junit.framework.TestSuite;
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.PrintStream;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import org.junit.After;
8+
import static org.junit.Assert.*;
69

710
/**
811
* Unit test for simple App.
912
*/
10-
public class AppTest
11-
extends TestCase
13+
public class AppTest
1214
{
13-
/**
14-
* Create the test case
15-
*
16-
* @param testName name of the test case
17-
*/
18-
public AppTest( String testName )
19-
{
20-
super( testName );
15+
16+
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
17+
18+
@Before
19+
public void setUpStreams() {
20+
System.setOut(new PrintStream(outContent));
2121
}
2222

23-
/**
24-
* @return the suite of tests being tested
25-
*/
26-
public static Test suite()
27-
{
28-
return new TestSuite( AppTest.class );
23+
@Test
24+
public void testAppConstructor() {
25+
try {
26+
new App();
27+
} catch (Exception e) {
28+
fail("Construction failed.");
29+
}
2930
}
3031

31-
/**
32-
* Rigourous Test :-)
33-
*/
34-
public void testApp()
32+
@Test
33+
public void testAppMain()
3534
{
36-
assertTrue( true );
35+
App.main(null);
36+
try {
37+
assertEquals("Hello World!" + System.getProperty("line.separator"), outContent.toString());
38+
} catch (AssertionError e) {
39+
fail("\"message\" is not \"Hello World!\"");
40+
}
3741
}
42+
43+
@After
44+
public void cleanUpStreams() {
45+
System.setOut(null);
46+
}
47+
3848
}

0 commit comments

Comments
 (0)