2017-09-19 9 views
1

私は、次のサービスがあります。モック他のサービスでのサービスのサービスJUnitの

@Service 
public class AccountServiceImpl implements AccountService { 

    @Autowired 
    protected ContractService contractService; 

    private void saveInCache(MultipartFile multipartFile) { 
     this.contractService.saveInCache(multipartFile); 
    } 
} 

と他のサービス

@Service 
public class ClientServiceImpl implements ClientService { 

    @Autowired 
    protected ContractService contractService; 

    private void getInfoOfFile(String multipartFileId) { 
     DocumentInfo document = this.contractService.getInfo(multipartFileId); 
     /// 
    } 
} 

を、私は私のJUnitの

public class ClientControllerTest extends ApiWebTest { 

    @Mock 
    protected ContractService contractService; 

    @Autowired 
    @InjectMocks 
    protected ClientService clientService = new ClientServiceImpl(); 

    @Before 
    private void setup() { 
    MockitoAnnotations.initMocks(this); 
    } 

    @Test 
    private void testGetInfo() { 
    // Code 
    DocumentInfo multipartFile = new DocumentInfo(); 
    multipartFile.setMultipartFileId(1234); 
    when(this.contractService.getInfo(multipartFile.getMultipartFileId())).thenReturn(multipartFile); 

    // Test the Client service 'getInfoOfFile' method. 
    } 
} 

私を持っていますこのテストをデバッグモードで実行すると、this.contractService.getInfo(multipartFileId);が私に 'null'を返していることがわかります。

ここで私は嘲笑に間違っています。

私はJUnitでContractServiceを嘲笑しました。私はAccountServiceImplでも模倣する必要がありますか?

EDIT:追加saveInCacheとGETINFO方法は

private DocumentInfo getInfo(String documentId) { 
     if (StringUtils.isEmpty(documentId)) { 
      return null; 
     } 
     WriteLock lock = this.readWriteLock.writeLock(); 
     try { 
      lock.lock(); 
      DocumentInfo cachedDocument = this.documentCache.get(documentId); 
      return cachedDocument; 
     } finally { 
      if (lock != null) { 
       lock.unlock(); 
      } 
     } 
    } 

private DocumentInfo saveInCache(StreamingStorage document) { 
     if (document == null) { 
      throw new InvalidParameterException("Creative document is required to put into cache."); 
     } 
     WriteLock lock = this.readWriteLock.writeLock(); 
     try { 
      lock.lock(); 
      DocumentInfo newCachedDocument = this.documentCache.put(document.getDocumentId(), document); 
      return newCachedDocument; 
     } finally { 
      if (lock != null) { 
       lock.unlock(); 
      } 
     } 
    } 
+1

保護されたClientServiceのclientServiceに@Autowiredは必要ありません。また、保護されたClientServiceの代わりにclientService。試してみてください - プライベートClientServiceImpl clientService – asg

+0

また、ContractServiceのgetInfoは見えません。あなたはその方法を追加してもらえますか? – asg

+0

@asgメソッドを追加しました – Vishnukk

答えて

0

私はあなたがclientServiceの宣言を自分で矛盾していると思います。

あなたは持っている:

@Autowired 
@InjectMocks 
protected ClientService clientService = new ClientServiceImpl(); 

これはclientServiceと呼ばれるautowired ClientServiceを作成し、モックを注入する必要があります。しかし、= new ClientServiceImpl()はautowiringを無効にしてプレーンなバニラを作ります(私は思っています!)。また、@Autowired@InjectMocksは同時に必要ではありません。あなたは、自動割り当てされたオブジェクトではなく、モックが挿入されたサービスを作成したいと思っています。

あなたは、このようにテストを変更してみてください:

@RunWith(MockitoJUnitRunner.class) 
public class ClientControllerTest extends ApiWebTest { 

    @Mock 
    protected ContractService contractService; 

    @InjectMocks 
    protected ClientService clientService; 

    @Test 
    private void testGetInfo() { 
    DocumentInfo multipartFile = new DocumentInfo(); 
    multipartFile.setMultipartFileId(1234); 
    when(this.contractService.getInfo(multipartFile)).thenReturn(multipartFile); 

    } 
} 

@RunWith(MockitoJUnitRunner.class)を追加するには、すべてのオブジェクトの作成は、あなたからの任意のさらなる作業を必要とせずに起こることを意味します。

+0

それ以前に試しました。まだ運がない! – Vishnukk

+0

もっと大きな例を追加しました:Autowiredを削除し、Runwithを追加しました。これは私のために働く – robjwilkins

0

@InjectMocksは、クラスのインスタンスを作成し、@Mock注釈で作成されたモックをそれに挿入します。したがって、ClientServiceのインスタンスを作成する必要はなく、@Autowiredを削除する必要があります。

MockitoAnnotations.initMocks(this)の代わりにMockitoJUnitRunnerを使用できます。コードは簡単です。

TestClassを変更後:

@RunWith(MockitoJUnitRunner.class) 
public class ClientControllerTest extends ApiWebTest { 

    @Mock 
    private ContractService contractService; 

    @InjectMocks 
    private ClientService clientService; 

    @Test 
    private void testGetInfo() { 
     // Code 
     DocumentInfo multipartFile = new DocumentInfo(); 
     multipartFile.setMultipartFileId(1234); 

     when(this.contractService.getInfo(multipartFile)).thenReturn(multipartFile); 

     // Test the Client service 'getInfoOfFile' method. 
    } 
} 
0
 DocumentInfo multipartFile = new DocumentInfo(); 
    multipartFile.setMultipartFileId(1234); 
    when(this.contractService.getInfo(multipartFile)).thenReturn(multipartFile); 
ここ

あなたがテスト中にDocumentInfoの別のインスタンスが存在しますので、以来、そうではありません、あなたのモックでmultipartFileインスタンスは(それを作成するgetInfo方法を参照してください期待)。

あなたはこのようなものにするために、あなたのモックを変更する必要があります。

when(this.contractService.getInfo(any())).thenReturn(multipartFile); 

この場合、期待はあなたがコンストラクタを介して作成する代わりに、あなたの特定のインスタンスのDocumentInfoの任意のインスタンスと照合されますmultipartFile = new DocumentInfo();

関連する問題