同時需要單元測試和集成測試。單元測試不應該在數據庫或文件等中使用。我喜歡使用Spring配置文件進行測試。例如,如果我有一個名爲integeration_test的配置文件。
@ActiveProfiles("integeration_test")
@ContextConfiguration(locations = {
"classpath:you-context.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public abstract class DaoTest
{
@Autowired
protected DataSource dataSource;
// delete all your stuff here
protected void clearDatabase()
{
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
jdbc.execute("delete table");
}
@Before
public final void init()
{
clearDatabase();
}
@After
public final void cleanup()
{
clearDatabase();
}
}
(我使用XML),然後在你的情況下做這樣的事情:<beans profile="test">TODO </beans>
,並在那裏配置數據源。
我知道在運行測試後有回滾所有事務的方法,但我更喜歡這樣。只是不要刪除真正的數據庫中的所有內容,甚至可以在clearDatabase中添加一些安全代碼,以確保不會發生。
對於性能測試,您真的需要弄清楚您想要達到的目標以及顯示的內容。如果你有關於性能測試的具體問題,你可以問,否則它是太廣泛的話題。
也許你可以製作一個迷你web應用程序,它可以爲你做性能測試,並將結果公開爲以HTML格式顯示的URL請求。真的只是取決於你願意花多少努力以及你想要測試什麼。