代碼清單
import org
import org
import com
@Service
public class FooService {
@Autowired
private FooDao dao;
public String save(String name){
if(name == null ||
throw new RuntimeException(
return dao
}
}
import org
@Repository
public class FooDao {
public String save(String name){
return
}
}
import org
AbstractDependencyInjectionSpringContextTests;
import com
public class MyTest extends AbstractDependencyInjectionSpringContextTests{
protected FooService fooService;
//set方法
public void setFooService(FooService fooService) {
this
}
//指定Spring配置文件的位置
protected String[] getConfigLocations(){
return new String[]{
}
//測試方法
public void testSave(){
String str = this
System
assertEquals(
}
}
<?xml version=
<beans xmlns=//…>
<context:component
</beans>
代碼清單
AbstractDependencyInjectionSpringContextTests類
代碼清單
public class MyTest extends AbstractDependencyInjectionSpringContextTests{
protected FooService fooService;
public MyTest(){
//啟用直接對屬性變量進行注入的機制
this
}
protected String[] getConfigLocations(){
return new String[]{
}
public void testSave(){
String str = this
System
assertEquals(
}
}
代碼清單
代碼清單
import org
AbstractTransactionalSpringContextTests;
import com
public class MyTest extends AbstractTransactionalSpringContextTests{
protected FooService fooService;
public MyTest(){
this
}
protected String[] getConfigLocations(){
return new String[]{
}
//測試方法中的數據操作將在方法返回前被回滾
public void testSave(){
String str = this
System
assertEquals(
}
}
這樣就可以在方法返回之前將測試數據回滾
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28838.html