User Manual¶
Simple example of the library¶
Let’s try to test the simplest example possible:
package com.nikolavp.approval.example;
/**
* User: nikolavp
* Date: 19/03/14
* Time: 19:16
*/
public class SimpleExample {
public static String generateHtml(String pageTitle) {
return String.format("<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
" <title>%s</title>\n" +
"<meta charset=\"utf-8\"/>\n" +
"<link href=\"css/myscript.css\"\n" +
" rel=\"stylesheet\"/>\n" +
"<script src=\"scripts/myscript.js\">\n" +
"</script>\n" +
"</head>\n" +
"<body>\n" +
"...\n" +
"</body>\n" +
"</html>", pageTitle);
}
}
now this class is not rocket science and if we want to test getResult(), we would something like the following in JUnit:
package com.nikolavp.approval.example;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* User: nikolavp
* Date: 19/03/14
* Time: 19:17
*/
public class SimpleExampleTest {
@Test
public void shouldReturnSomethingToTestOut() throws Exception {
//arrange
String title = "myTitle";
String expected = "<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
" <title>" + title +"</title>\n" +
"<meta charset=\"utf-8\"/>\n" +
"<link href=\"css/myscript.css\"\n" +
" rel=\"stylesheet\"/>\n" +
"<script src=\"scripts/myscript.js\">\n" +
"</script>\n" +
"</head>\n" +
"<body>\n" +
"...\n" +
"</body>\n" +
"</html>";
//act
String actual = SimpleExample.generateHtml(title);
//assert
assertThat(actual, equalTo(expected));
}
}
this is quite terse and short. Can we do better? Actually approval is not any shorter:
package com.nikolavp.approval.example;
import com.nikolavp.approval.Approval;
import com.nikolavp.approval.reporters.Reporters;
import org.junit.Test;
import java.nio.file.Paths;
/**
* User: nikolavp
* Date: 19/03/14
* Time: 18:11
*/
public class SimpleExampleApprovalTest {
@Test
public void shouldReturnSomethingToTestOut() throws Exception {
//assign
Approval<String> approver = Approval.of(String.class)
.withReporter(Reporters.console())
.build();
String title = "myTitle";
//act
String actual = SimpleExample.generateHtml(title);
//verify
approver.verify(actual, Paths.get("./test.txt"));
}
}
this is not the best example