私は、次のサービスがあります。モック他のサービスでのサービスのサービス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();
}
}
}
保護されたClientServiceのclientServiceに@Autowiredは必要ありません。また、保護されたClientServiceの代わりにclientService。試してみてください - プライベートClientServiceImpl clientService – asg
また、ContractServiceのgetInfoは見えません。あなたはその方法を追加してもらえますか? – asg
@asgメソッドを追加しました – Vishnukk