2016-02-24 50 views
0

我有一個項目結構控制器 - >響應構建器 - >服務層。 這裏服務層調用存儲庫層(數據庫層)。按照結構時直接從主類調用服務層

一切正常。

但是,對於測試我想服務層直接從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可以做到這一點???????

+0

使用junit進行測試,而不是主要類。如果要從主類運行服務邏輯,則必須構建應用程序上下文並從中獲取服務bean。 – Jens

+0

你的意思是「這個過程不起作用」 - 可能更具體嗎?任何堆棧跟蹤? – mlewandowski

回答

1

只要你使用彈簧,你必須從來沒有使用new來建立一個應該由spring管理的對象。

所以,你可以:

  • 要麼做手工,這意味着引導與同intialization應用程序上下文,它會在你的應用程序,並明確地從它

    ApplicationContext ctx = new AnnotationConfigApplicationContext(
        Class<?>... annotatedClasses); // if it is the way you use it ... 
    IshmamAddressInterface i = ctx.getBean("ishmamAddressServiceImpl", 
        IshmamAddressInterface .class); 
    
  • 取得bean
  • 或使用Spring test framework它會自動爲你做Junit測試

    @WebAppConfiguration 
    @ContextConfiguration(classes = ...) 
    public class WebIntegrationTests { 
    
        @Autowired 
        @Qualifier("ishmamAddressServiceImpl") 
        IshmamAddressInterface ishmamAddressService; 
        ... 
        @Test 
        public void testGetAddress() { 
         ishmamAddressService.getAddress(); 
         ... 
        } 
    } 
    
+0

@ContextConfiguration(類= ......)......是什麼在參數(類= .. ????) –

+0

@IshmamShahriar:這將是太長了SO回答,解釋如何使春天集成測試與Junit,這就是爲什麼我給你一個指向良好的Spring框架文檔的指針。如果您以前沒有使用過,那麼您應該閱讀該文檔。 –