Skip to content

Instantly share code, notes, and snippets.

@Darker
Last active March 8, 2017 17:16
Show Gist options
  • Save Darker/5ff47804ea1b8502583e95b2a4079b02 to your computer and use it in GitHub Desktop.
Save Darker/5ff47804ea1b8502583e95b2a4079b02 to your computer and use it in GitHub Desktop.
Example of JUnit test for CTU PJV homework assignment 01
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import static org.junit.Assert.*;
import org.junit.Before;
/**
*
* @author mareda
*/
public abstract class HomeworkTestBase {
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
// <3 Thanks to http://stackoverflow.com/q/1119385/607407
protected final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
// sterr not used yet, but might be useful in future assignments
protected final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
/*
@returns Output without \r characters which are added on Windows.
*/
protected String sanitizedOutput() {
return outContent.toString().replace("\r", "");
}
/**
* Override this to execute homework assignment internal code (which generates std io)
*/
protected abstract void runHW();
protected void testHWBasic(String input, String expected_output) {
simulateIn(input);
// Homework assignment executes here
runHW();
assertEquals(expected_output, sanitizedOutput());
}
public static void simulateIn(final String str) {
// Simulated STDIN
final ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes());
System.setIn(in);
}
@Before
public void setUpStreams() {
System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
}
@After
public void cleanUpStreams() {
System.setOut(null);
System.setErr(null);
System.setIn(null);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cz.cvut.fel.pjv</groupId>
<artifactId>lab01</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<!-- Important shit here: -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<src.dir>src/</src.dir>
<test.dir>test/</test.dir>
<resource.dir>resources/</resource.dir>
</properties>
</project>
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import org.junit.Test;
import static org.junit.Assert.*;
import cz.cvut.fel.pjv.Lab01;
/**
*
* @author mareda
*/
public class TestCalculator extends HomeworkTestBase {
public TestCalculator() {
}
/**
* Shorthand for formated number output
* @param number
* @param precision Number of decimal places to show. Trailing zeros will appear to fully fill the number of requested places
* @return
*/
protected static String forNum(double number, int precision) {
final String precisionFormula = "%."+Integer.toString(precision)+"f";
return String.format(precisionFormula, number);
}
protected static String formatResult(double A, String operator, double B, double result, int precision) {
return forNum(A,precision)+" "+operator+" "+forNum(B,precision)+" = "+forNum(result,precision)+"\n";
}
@Override
protected void runHW() {
new Lab01().start(new String[] {});
}
protected static final String INTRO = "Vyber operaci (1-soucet, 2-rozdil, 3-soucin, 4-podil):\n";
protected static final String SCITANEC = "Zadej scitanec: \n";
protected static final String CINITEL = "Zadej cinitel: \n";
protected static final String DELENEC = "Zadej delenec: \n";
protected static final String DELITEL = "Zadej delitel: \n";
protected static final String MENSENEC = "Zadej scitanec: \n";
protected static final String MENSITEL = "Zadej scitanec: \n";
protected static final String SCITANI = SCITANEC+SCITANEC;
protected static final String NASOBENI = CINITEL+CINITEL;
protected static final String ODCITANI = MENSENEC+MENSITEL;
protected static final String DELENI = DELENEC+DELITEL;
protected static final String ERR_DIVIDE_BY_ZERO = "Pokus o deleni nulou!\n";
protected static final String ERR_INVALID_CHOICE = "Chybna volba!\n";
protected static final String ERR_NEGATIVE_PRECISION = "Chyba - musi byt zadane kladne cislo!\n";
protected static final String PRECISION = "Zadej pocet desetinnych mist: \n";
@Test
public void TestOnePlusOne() throws Exception {
testHWBasic("1 1 1 1", INTRO + SCITANI+ PRECISION + formatResult(1, "+", 1, 2, 1));
}
@Test
public void TestDivideByZero() throws Exception {
testHWBasic("4 1 0 1", INTRO + DELENI + ERR_DIVIDE_BY_ZERO);
}
@Test
public void TestIntDivide() throws Exception {
testHWBasic("4 1 10 0", INTRO + DELENI + PRECISION + formatResult(1, "/", 10, 0, 0));
}
@Test
public void TestInvalidChoice() throws Exception {
testHWBasic("5 1 10 0", INTRO + ERR_INVALID_CHOICE);
}
@Test
public void TestInvalidPrecision() throws Exception {
testHWBasic("1 1 10 -5", INTRO + SCITANI + PRECISION + ERR_NEGATIVE_PRECISION);
}
@Test
public void TestDoubleMultiply() throws Exception {
double A = 12.345678;
double B = 1.23456;
testHWBasic("3 "+String.format("%f", A)+" "+String.format("%f", B)+" 3", INTRO + NASOBENI + PRECISION + formatResult(A, "*", B, 15.241, 3));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment