File tree 3 files changed +48
-30
lines changed
main/java/com/mycompany/app
test/java/com/mycompany/app
3 files changed +48
-30
lines changed Original file line number Diff line number Diff line change 11
11
<dependency >
12
12
<groupId >junit</groupId >
13
13
<artifactId >junit</artifactId >
14
- <version >3.8.1 </version >
14
+ <version >4.11 </version >
15
15
<scope >test</scope >
16
16
</dependency >
17
17
</dependencies >
Original file line number Diff line number Diff line change 2
2
3
3
/**
4
4
* Hello world!
5
- *
6
5
*/
7
- public class App
6
+ public class App
8
7
{
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 ());
12
15
}
16
+
17
+ private final String getMessage () {
18
+ return message ;
19
+ }
20
+
13
21
}
Original file line number Diff line number Diff line change 1
1
package com .mycompany .app ;
2
2
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 .*;
6
9
7
10
/**
8
11
* Unit test for simple App.
9
12
*/
10
- public class AppTest
11
- extends TestCase
13
+ public class AppTest
12
14
{
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 ));
21
21
}
22
22
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
+ }
29
30
}
30
31
31
- /**
32
- * Rigourous Test :-)
33
- */
34
- public void testApp ()
32
+ @ Test
33
+ public void testAppMain ()
35
34
{
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
+ }
37
41
}
42
+
43
+ @ After
44
+ public void cleanUpStreams () {
45
+ System .setOut (null );
46
+ }
47
+
38
48
}
You can’t perform that action at this time.
0 commit comments