我有一個項目結構控制器 - >響應構建器 - >服務層。 這裏服務層調用存儲庫層(數據庫層)。按照結構時直接從主類調用服務層
一切正常。
但是,對於測試我想服務層直接從Java主類調用。
我怎樣才能做到這一點??????
我的控制器:
@RestController
@RequestMapping("/ishmam")
public class IshmamAddressController {
@Autowired
@Qualifier("ishmamAddressBuilder")
IshmamAddressBuilder ishmamAddressBuilder;
@RequestMapping("/getAddress")
public IResponseDto<WebbCustomerAddressDto> getAllAddress(){
return ishmamAddressBuilder.getAllAddress();
}
}
我的助洗劑類是:
@Component("ishmamAddressBuilder")
public class IshmamAddressBuilder {
@Autowired
@Qualifier("ishmamAddressServiceImpl")
IshmamAddressInterface ishmamAddressService;
public IResponseDto<IshmamAddressResponseDto> getAllAddress(){
IResponseDto<WebbCustomerAddressDto> response=new
IResponseDto<WebbCustomerAddressDto>();
try{
//here i call the service layer
List<String> addressList=ishmamAddressService.getAllAddress();
}catch(Exception e){
throw e;
}
return addressList;
}
我的服務層是:
@Service("ishmamAddressServiceImpl")
@Transactional
public class IshmamAddressServiceImpl implements IshmamAddressInterface {
@Autowired(required = true)
@Qualifier("webCustomerAddressRepository")
WebCustomerAddressRepository webCustomerAddressRepository;
@Override
public IshmamAddressResponseDto getAllAddress() {
List<WebCustomerAddress> aList = new ArrayList<WebCustomerAddress>();
List<WebbCustomerAddressDto> dtoWebCustomerAddressList = new
ArrayList<WebbCustomerAddressDto>();
IshmamAddressResponseDto ishmamAddressResponseDto=new
IshmamAddressResponseDto();
try{
aList =
address.getAllAddress(1);//Calling database layer
ishmamAddressResponseDto=//Doing something,not important for
//question
}
return ishmamAddressResponseDto;
}
現在我要的是服務層直接從主級呼叫:
public class Address{
public void getAddress(){
IshmamAddressServiceImpl i=new IshmamAddressServiceImpl();
List<String> list=i.getAllAddress();
}
public static void main(String[] args){
Address a=new Address();
a.getAddress();
}
}
這個過程不是我working.How可以做到這一點???????
使用junit進行測試,而不是主要類。如果要從主類運行服務邏輯,則必須構建應用程序上下文並從中獲取服務bean。 – Jens
你的意思是「這個過程不起作用」 - 可能更具體嗎?任何堆棧跟蹤? – mlewandowski