Ich habe den folgenden Service:Mock ein Dienst von einem Dienst in einem anderen Dienst Junit
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
protected ContractService contractService;
private void saveInCache(MultipartFile multipartFile) {
this.contractService.saveInCache(multipartFile);
}
}
und einen anderen Dienst
@Service
public class ClientServiceImpl implements ClientService {
@Autowired
protected ContractService contractService;
private void getInfoOfFile(String multipartFileId) {
DocumentInfo document = this.contractService.getInfo(multipartFileId);
///
}
}
und ich habe meine 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.
}
}
Wenn i Führen Sie diesen Test im Debug-Modus, sehe ich, dass die this.contractService.getInfo(multipartFileId);
mir 'Null' zurückgibt.
Wohin gehe ich falsch im Spott.
Ich habe gerade den ContractService in meiner JUnit verspottet. Muss ich auch den AccountServiceImpl verspotten?
EDIT: die saveInCache und getInfo Methoden
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();
}
}
}
Sie brauchen @Autowired nicht für geschützten ClientService clientService; Auch anstelle des geschützten ClientService clientService; versuch mit - private ClientServiceImpl clientService – asg
Auch getInfo von ContractService ist nicht sichtbar. Könnten Sie bitte diese Methode hinzufügen? – asg
@asg Hinzugefügt die Methoden – Vishnukk