Skip to content

Commit

Permalink
Simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkEWaite committed Dec 20, 2022
1 parent 635d3f8 commit 7866331
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 37 deletions.
9 changes: 4 additions & 5 deletions src/main/java/com/mycompany/app/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
*/
public class App {

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

public App() {}

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

private final String getMessage() {
return message;
public String getMessage() {
return MESSAGE;
}

}
38 changes: 6 additions & 32 deletions src/test/java/com/mycompany/app/AppTest.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,25 @@
package com.mycompany.app;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

/**
* Unit test for simple App.
*/
public class AppTest
{

private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();

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

@Test
public void testAppConstructor() {
try {
new App();
} catch (Exception e) {
fail("Construction failed.");
}
App app1 = new App();
App app2 = new App();
assertEquals(app1.getMessage(), app2.getMessage());
}

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

@AfterEach
public void cleanUpStreams() {
System.setOut(null);
App app = new App();
assertEquals("Hello World!", app.getMessage());
}

}

0 comments on commit 7866331

Please sign in to comment.