package comp1110.exam;
import org.junit.*;
import org.junit.rules.Timeout;
Copyright By PowCoder代写 加微信 powcoder
import org.junit.runners.MethodSorters;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class P1HelloWorldTest {
public Timeout globalTimeout = Timeout.millis(1000);
public void testHasHello() {
P1HelloWorld.main(null);
assertTrue(“Does not print hello”, (outContent.toString().toLowerCase().lastIndexOf(“hello”) >= 0));
public void testHasWorld() {
P1HelloWorld.main(null);
assertTrue(“Does not print world”, (outContent.toString().toLowerCase().lastIndexOf(“world”) >= 0));
public void testHasExactlyOneSpace() {
P1HelloWorld.main(null);
assertTrue(“Has wrong number of spaces”, (outContent.toString().contains(” “) && outContent.toString().lastIndexOf(” “) == outContent.toString().indexOf(” “)));
public void testEndsWithExclamation() {
P1HelloWorld.main(null);
Pattern p = Pattern.compile(“!$”);
Matcher m = p.matcher( outContent.toString().toLowerCase() );
assertTrue(“Does not end with exclamation mark”, m.find());
public void testCorrectCase() {
P1HelloWorld.main(null);
assertTrue(“Incorrect use of upper and lower case”, (outContent.toString().lastIndexOf(“Hello”) >= 0 && outContent.toString().lastIndexOf(“world”) >= 0));
public void testExactlyCorrect() {
P1HelloWorld.main(null);
Pattern p = Pattern.compile(“^Hello world!\\s+$”);
Matcher m = p.matcher( outContent.toString());
assertTrue(“Text not precisely the same”+”-“+outContent.toString()+”-“, m.matches());
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
public void setUpStreams() {
System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
public void cleanUpStreams() {
System.setOut(null);
System.setErr(null);
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com