[JUnit] @Rule TemporaryFolder 사용하기
모하니?/Coding : 2009. 10. 16. 16:26
public class DBUnitSupportTest {
DBUnitSupport dbUnitSupport;
@Rule public TemporaryFolder folder = new TemporaryFolder();
@Autowired DataSource dataSource;
File tempFile;
@Before
public void setUp() throws Exception{
dbUnitSupport = new DBUnitSupport(dataSource);
}
@Test
public void exportXls() throws Exception {
tempFile = folder.newFile("temp.xls");
dbUnitSupport.setExportedFile(tempFile);
assertThat(tempFile.length(), is(0L));
dbUnitSupport.exportXlsFrom("code", "item", "users", "role", "users_role");
assertThat(tempFile.length(), not(0L));
}
@After
public void after(){
assertFalse(tempFile.exists());
}
DBUnitSupport dbUnitSupport;
@Rule public TemporaryFolder folder = new TemporaryFolder();
@Autowired DataSource dataSource;
File tempFile;
@Before
public void setUp() throws Exception{
dbUnitSupport = new DBUnitSupport(dataSource);
}
@Test
public void exportXls() throws Exception {
tempFile = folder.newFile("temp.xls");
dbUnitSupport.setExportedFile(tempFile);
assertThat(tempFile.length(), is(0L));
dbUnitSupport.exportXlsFrom("code", "item", "users", "role", "users_role");
assertThat(tempFile.length(), not(0L));
}
@After
public void after(){
assertFalse(tempFile.exists());
}
흠.. 좋군요!! 파일이 생겨나는 테스트를 하고 나서 매번 수동이든 코드로든 지워야 하는 번거로움이 없어졌습니다.
사실 파란색 코드는 지워도 그만인데, 아직은 TemporaryFolder Rule이 어떻게 동작하는지 확실하게 파악된 상태가 아니여서 남겨뒀습니다.
파일이 delete() 되지 않을 경우에 대비해서 말이죠. 사실 이 아래 글에 작성한 코드에는 치명적인 결함이 있었는데, 그 사실을 이번 테스트를 작성하다가 알게됐습니다. @_@;;
테스트 작성이 여러모로 도움을 주는군요.
참조: http://www.catosplace.net/blogs/personal/?p=116
'모하니? > Coding' 카테고리의 다른 글
[봄싹] 모임 추가 시나리오 - web flow (0) | 2009.10.23 |
---|---|
[Spring Wev Flow(2.0.8)] SecurityFlowExecutionListener 패치 for Spring Security 3.X (0) | 2009.10.22 |
[코드값] 애플리케이션에 DB id값이... (6) | 2009.10.21 |
[봄싹] 메일 서비스를 알림 서비스로 통합하기 (0) | 2009.10.20 |
[리팩토링] 메일을 메시지로 통합하기 (0) | 2009.10.20 |
[JUnit] @Rule TemporaryFolder 사용하기 (2) | 2009.10.16 |
[DBUnit] 테이블 데이터를 엑셀로 export (0) | 2009.10.16 |
[기초 데이터] 백업/복구 방안 (0) | 2009.10.15 |
[봄싹 버그]] JSON 뷰와 하이버가 가져온 Proxy 객체 (0) | 2009.10.15 |
[권한] 3단 구조 (0) | 2009.10.14 |
[자바스크립트] confirm() 주의할 것 (0) | 2009.10.13 |
어후, 이거 좋네요. ^^
매번 테스트 할때마다 디렉토리 만들고 지우고 난리도 아니었는데 ㅠㅠ;
네 사용법도 간단하고 좋아요