🔖 오늘 읽은 범위 : 9장, 단위 테스트
이전 내용 복습
- 깨끗한 테스트 코드를 만들려면? 세 가지가 필요하다. 가독성,가독성,가독성
- 테스트 코드는 최소의 표현으로 많은 것을 나타내야 한다.*
public void testGetPageHieratchyAsXml() throws Exception
{
// PathParser는 문자열을 pagePath 인스턴스로 변환한댜.
// pagePathsms 웹 로봇(crawler)이 사용하는 객체다.
crawler.addPage(root, PathParser.parse("PageOne") ) ;
crawler.addPage(root, PathParser.parse("PageOne.ChildOne") ) ;
crawler.addPage(root, PathParser.parse("PageTwo") ) ;
request.setResource("root") ;
request.addlnput("type", "pages");
// 이 영역도 PathParser와 마찬가지로 잡음에 불과하다
Responder responder = new SerializedPageResponder();
SimpleResponse response =
(SimpleResponse) responder.makeResponse(
new FitNesseContext(root), request);
String xml = response.getContent();
assertEquals("text/xml", response.getContentType());
assertSubString("<name>PageOne</name>", xml);
assertSubString("<name>PageTwo</name>", xml);
assertSubString("<name>ChildOne</name>", xml) ;
}
public void testGetPageHieratchyAsXmlDoesntContainSymbolicLinks() throws Exception
{
WikiPage pageOne = crawler.addPage(root, PathParser.parse("Pageone") ;
crawler.addPage(root, PathParser.parse("PageOne.ChildOne") ) ;
crawler.addPage(root, PathParser.parse("PageTwo") ) ;
PageData data = pageOne.getData();
WikiPageProperties properties = data.getProperties( ) ;
WikiPageProperty symlinks = properties.set(SymbolicPage.PROPERTY_NAME) ;
symLinks.set("SymPage", "PageTwo") ;
pageOne.commit(data) ;
request.setResource("root") ;
request.addInput("type", "pages") ;
Responder responder = new SerializedPageResponder( ) ;
SimpleResponse response =
( SimpleResponse ) responder.makeResponse (
new FitNesseContext(root) , request ) ;
String xml = response.getContent() ;
assertEquals("text/xml", response.getContentType());
assertSubString("<name>PageOne</name>", xml) ;
assertSubString("<name>PageTwo</name>", xml) ;
assertSubString("<name>ChildOne</name>", xml) ;
assertNotSubString("SymPage", xml) ;
}
public void testGetDataAsHtml() throws Exception
{
crawler.addPage(root, PathParser.parse("TestPageOne") , "test page") ;
request.setResource("TestPageOne") ;
request.addInput("type", "data") ;
Responder responder = new SerializedPageResponder();
SimpleResponse response =
(SimpleResponse) responder.makeResponse(
new FitNesseContext(root) , request) ;
String xml = response.getContent();
assertEquals("text/xml" , response.getContentType( ) ) ;
assertSubString( "test page" , xml) ;
assertSubString( "<Test" , xml);
}
public void testGetPageHierarchyAsXml() throws Exception {
makePages("PageOne", "PageOne.ChildOne", "PageTwo");
submitRequest("root", "type:pages");
assertResponseIsXML();
assertResponseContains(
"<name>PageOne</name>" , "<name>PageTwo</name>" , "<name>ChildOne</name>"
);
}
public void testSymboliclinksAreNotlnXmlPageHierarchy( ) throws Exception {
WikiPage page = makePage("PageOne");
makePages("PageOne.ChildOne", "PageTwo");
addlinkTo(page, "PageTwo", "SymPage");
submitRequest("root", "type:pages");
assertResponseIsXML( ) ;
assertResponseContains(
"<name>PageOne</name>", "<name>PageTwo</name>", "<name>ChildOne</name>"
);
assertResponseDoesNotContain("SymPage");
}
public void testGetDataAsXml() throws Exception {
makePageWithContent("TestPageOne", "test page");
submitRequest("TestPageOne", "type:data");
assertResponseIsXML() ;
assertResponseContains("test page", "<Test");
}
@Test
public void turnOnLoTempAlarmAtThreashold() throws Exception {
hw.setTemp(WAY_TO_COLD);
// 문제점1. tic() 함수가 무엇일까? 궁금하게 함
controller.tic();
// 문제점2. 코드를 점검하는 상태 이름과 상태 값을 확인하느라 눈길이 여기저기 흩어진다.
assertTrue(hw.heaterState());
assertTrue(hw.blowerState());
assertFalse(hw.coolerState());
assertFalse(hw.hiTempAlarm());
assertTrue(hw.loTempAlarm());
}
@Test
public void turnOnLoTempAlarmAtThreshold() throws Exception {
wayTooCold();
// 실제 환경에서는 안되지만, 테스트 환경에서는 문제 없는 코드!
// 일단 "HBchL"이 의미하는 바만 안다면 눈길이 문자열을 따라 움직이며 결과를 재빨리 판단한다.
assertEquals("HBchL", hw.getState());
}
public void testGetPageHierarchyAsXml( ) throws Exception {
givenPages("PageOne", "PageOne.ChildOne", "PageTwo");
whenRequestIsissued("root", "type:pages");
// assert1. 출력이 XML이다.
thenResponseShouldBeXML();
}
public void testGetPageHierarchyHasRightTags( ) throws Exception {
givenPages("PageOne", "PageOne.ChildOne", "PageTwo");
whenRequestIsIssued("root", "type: pages") ;
// assert2. 특정 문자열을 포함한다.
thenResponseShouldContain(
"<name>PageOne</name>", "<name>PageTwo</name>", "<name>ChildOne</name>"
);
}