HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Simple Hello World with unit tests

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
simplewithtestsworldhellounit

Problem

This is a simple hello world sample project used for instruction. I'm just looking for general comments on the approach and design.

AdvancedHelloWorldTest.java

/**
 * AdvancedHelloWorldTest.java
 * 2014 GetGnosis.com
 */
package com.getgnosis.tutorials.tutorial01001;

import static org.junit.Assert.*;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.junit.Test;

/**
 *  Test Class for 
 *  {@link com.getgnosis.tutorials.tutorial01001.AdvancedHelloWorld}
 *
 * @author mhemphill
 * @version 1.0.0 - Mar 9, 2014
 */
public class AdvancedHelloWorldTest {

    /**
     * Test method for 
     * {@link com.getgnosis.tutorials.tutorial01001.AdvancedHelloWorld
     *  #AdvancedHelloWorld()}.
     */
    @Test
    public void testAdvancedHelloWorld() {
        try {
            new AdvancedHelloWorld();
        } catch (Exception e) {
            fail("Construction failed. ");
        }
    }

    /**
     * Test method for 
     * {@link com.getgnosis.tutorials.tutorial01001.AdvancedHelloWorld
     *      #main(java.lang.String[])}.
     */
    @Test
    public void testMain() {
        final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
        System.setOut(new PrintStream(outContent));
        AdvancedHelloWorld.main(null);
        assertEquals("Hello World!" + System.getProperty("line.separator"), outContent.toString());
        System.setOut(null);
    }

    /**
     * Test method for 
     * {@link com.getgnosis.tutorials.tutorial01001.AdvancedHelloWorld
     *      #toString()}.
     */
    @Test
    public void testToString() {
        String helloWorldString =  new AdvancedHelloWorld().toString();
        assertEquals("AdvancedHelloWorld [message=Hello World!]",helloWorldString);
    }

}


AdvancedHelloWorld.java

```
/**
* AdvancedHelloWorld.java
* 2014 GetGnosis.com
*/
package com.getgnosis.tutorials.tutorial01001;

/**
* This is an example program that prints the {@link String} "Hello World!" to
*

Solution

Having tests is great (I'd love to see more questions with tests), keep it up! Other parts I would keep simpler.

-
You might want this as a tag in your revision control system instead of commenting:

* @version 1.0.0 - Mar 9, 2014


This is also in the revision control history, so it's usually unnecessary:

* @author mhemphill


See: Pros and cons of versioning javadoc

-
I would not want to maintain comments on test methods like this:

/**
 * Test method for 
 * {@link com.getgnosis.tutorials.tutorial01001.AdvancedHelloWorld
 *      #toString()}.
 */
@Test
public void testToString() {
    ...
}


I don't see why would it be useful. I haven't seen anyone who likes reading javadoc of test classes instead of the test code directly. The name of the method is good, it contains the main point: it's a test for toString, keep that and remove the superfluous comment.

-

@Test
public void testMain() {
    final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
    System.setOut(new PrintStream(outContent));
    AdvancedHelloWorld.main(null);
    assertEquals("Hello World!" + System.getProperty("line.separator"), outContent.toString());
    System.setOut(null);
}


The System.setOut(null) statements should be in an @After tearDown method or at least in a finally block. If the assertEquals throws an exception (or something else) out won't be set back.

-
I the same testMain method I'd create a local variable for the expected result:

final String expected = "Hello World!" + System.getProperty("line.separator");
assertEquals(expected, outContent.toString());


It's easier to read. (Clean Code by Robert C. Martin, G19: Use Explanatory Variables; Refactoring: Improving the Design of Existing Code by Martin Fowler, Introduce Explaining Variable)

-

@Test
public void testAdvancedHelloWorld() {
    try {
        new AdvancedHelloWorld();
    } catch (Exception e) {
        fail("Construction failed. ");
    }
}


The following is the same:

@Test
public void testAdvancedHelloWorld() {
    new AdvancedHelloWorld();
}


If the constructor throws an exception (or anything inside a @Test method) JUnit will tell you.

-
This comments does not say anything which isn't in the code already, I'd remove it:

/**
 * The default constructor. Initializes the value of message via the static
 * block.
 */


(Clean Code by Robert C. Martin: Chapter 4: Comments, Noise Comments)

-

static {
    message = "Hello World!";
}

/**
 * The {@link String} instance representing the message to be displayed.
 */
private final static String message;


The following is the same:

private final static String message = "Hello World!";


I would keep it simple.

-

@Override
public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("AdvancedHelloWorld [");
    if (getMessage() != null)
        builder.append("message=").append(getMessage());
    builder.append("]");
    return builder.toString();
}


The if condition can't be false (the message field is final and has a non-null value), so the following is the same:

StringBuilder builder = new StringBuilder();
builder.append("AdvancedHelloWorld [");
builder.append("message=").append(getMessage());
builder.append("]");
return builder.toString();


-
I'd move the main method to a separate class. It's usually a good idea to separate a class from its clients.

Code Snippets

* @version 1.0.0 - Mar 9, 2014
* @author mhemphill
/**
 * Test method for 
 * {@link com.getgnosis.tutorials.tutorial01001.AdvancedHelloWorld
 *      #toString()}.
 */
@Test
public void testToString() {
    ...
}
@Test
public void testMain() {
    final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
    System.setOut(new PrintStream(outContent));
    AdvancedHelloWorld.main(null);
    assertEquals("Hello World!" + System.getProperty("line.separator"), outContent.toString());
    System.setOut(null);
}
final String expected = "Hello World!" + System.getProperty("line.separator");
assertEquals(expected, outContent.toString());

Context

StackExchange Code Review Q#43868, answer score: 12

Revisions (0)

No revisions yet.