Skip to content

Commit

Permalink
Refactor App and made AppTest properly test App.
Browse files Browse the repository at this point in the history
* Update version of junit dependency in 'pom.xml' file (to 4.11) so that
org.junit dependencies in AppTest could be imported.
  • Loading branch information
gilesgas committed Dec 18, 2017
1 parent b132e29 commit 793c42e
Showing 3 changed files with 48 additions and 30 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
18 changes: 13 additions & 5 deletions src/main/java/com/mycompany/app/App.java
Original file line number Diff line number Diff line change
@@ -2,12 +2,20 @@

/**
* Hello world!
*
*/
public class App
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );

private final String message = "Hello World!";

public App() {}

public static void main(String[] args) {
System.out.println(new App().getMessage());
}

private final String getMessage() {
return message;
}

}
58 changes: 34 additions & 24 deletions src/test/java/com/mycompany/app/AppTest.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,48 @@
package com.mycompany.app;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.Before;
import org.junit.Test;
import org.junit.After;
import static org.junit.Assert.*;

/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
public class AppTest
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );

private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();

@Before
public void setUpStreams() {
System.setOut(new PrintStream(outContent));
}

/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
@Test
public void testAppConstructor() {
try {
new App();
} catch (Exception e) {
fail("Construction failed.");
}
}

/**
* Rigourous Test :-)
*/
public void testApp()
@Test
public void testAppMain()
{
assertTrue( true );
App.main(null);
try {
assertEquals("Hello World!" + System.getProperty("line.separator"), outContent.toString());
} catch (AssertionError e) {
fail("\"message\" is not \"Hello World!\"");
}
}

@After
public void cleanUpStreams() {
System.setOut(null);
}

}

0 comments on commit 793c42e

Please sign in to comment.