2013-10-14 15 views
4

내 IT 중 하나에서 Springockito를 사용하여 DAO bean을 모의하고 싶습니다. 내 IT에서는 spring context.xml을 사용하여 일부 서비스를 autowire하고 mockApplication.xml을 사용하여 DAO를 조롱해야한다. 그래서, 어떻게 동시에 두 개의 XML 구성 파일을 사용할 수 있습니까? 당신이 수도로에서 지정할 수스프링 킷토하는 방법?

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(loader = SpringockitoContextLoader.class, locations = {"classpath*:/MockApplicationContext.xml"}) 
public class PayRollComponentFacadeIT { 
    @Autowired 
    IPayRollComponentFacade payRollComponentFacade; 
    @ReplaceWithMock 
    @Autowired 
    IPayRollPersistenceManager payRollPersistenceManager; 

내가 @ContextConfiguration(loader = SpringockitoContextLoader.class, locations = {"classpath*:/MockApplicationContext.xml"})

등 모의 상황을 포함했다 그러나 나는 또한 스프링 컨텍스트 @ContextConfiguration(locations = {"classpath*:/testApplicationContext.xml"})

감사 Rajib

답변

1

ContextConfiguration.locations을 포함 할 필요는 배열입니다 네가 원하는 locaction. BTW

@ContextConfiguration(
     loader = SpringockitoContextLoader.class, 
     locations = {"classpath*:/MockApplicationContext.xml", 
        "classpath*:/testApplicationContext.xml"} 
) 

: (이 내 기억에서 불과 힌트, 나는 문제가 계속 존재하는 경우 몰라요, 아니면 내가 뭔가 잘못 한 경우는) 오래 전에 나는 때 몇 가지 문제를 발견 두 개의 위치 매개 변수를 사용하여 스프링이 두 개의 컨텍스트 (각 위치에 하나씩)를 작성하기 때문에. 따라서 하나의 구성 파일을 사용하여 두 개의 일반 구성 파일 인 inculde을 사용합니다. (@https://stackoverflow.com/a/3414669/280244)

4

스프링 키토 주석은 추가 모의 상황을 전혀 피할 수 있습니다.

그냥 DAO는 동일한 테스트 케이스에 조롱하는 열거 :

@ReplaceWithMock 
DAO dao; 

이 DAO가 자동으로 기본 응용 프로그램 컨텍스트에서 대체 될 것입니다. 컨트롤러는 조롱 된 bean을 볼 수 있습니다.

+0

@ @ ReplaceWithMock' 이전에는'@ Autowired'가 필요합니다 !! – MariuszS

+2

'@ Autowired'는 테스트 케이스 클래스에서 조롱 된 인스턴스에 직접 액세스해야하는 경우에만 필요합니다. 봄 컨텍스트에서 bean을 mock으로 대체하기 만하면됩니다. 다른 모든 콩은 조롱 된 버전을 얻을 것입니다. Springockito 주석의 가장 멋진 기능입니다. – Vadzim

+1

와우, 멋진 기능, 감사합니다 :) – MariuszS