首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在另一个服务Junit中模拟服务的服务

在另一个服务Junit中模拟服务的服务
EN

Stack Overflow用户
提问于 2017-09-19 14:02:06
回答 4查看 8.1K关注 0票数 6

我有以下服务:

代码语言:javascript
复制
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    protected ContractService contractService;

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

和另一项服务

代码语言:javascript
复制
@Service
public class ClientServiceImpl implements ClientService {

    @Autowired
    protected ContractService contractService;

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

我有我的Junit

代码语言:javascript
复制
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吗?

编辑:添加saveInCache和getInfo方法

代码语言:javascript
复制
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();
            }
        }
    }
EN

Stack Overflow用户

发布于 2019-07-31 12:20:31

根本原因:您没有将ContractService注入ClientService。

我认为使用ReflectionTestUtils.可以更容易地解决这个问题。这意味着您将向AccountServiceImpl注入模拟的ContractService。

代码语言:javascript
复制
public class ClientControllerTest extends ApiWebTest {
  protected ClientService clientService = new ClientServiceImpl();
  private ContractService contractService;
  @Test
  private void testGetInfo() 
  {
     // Code
     DocumentInfo multipartFile = new DocumentInfo();
     multipartFile.setMultipartFileId(1234);
     contractService= EasyMock.createNiceMock(ContractService.class);
     ReflectionTestUtils.setField(clientService, "contractService", contractService);
     EasyMock.expect(contractService.getInfo(multipartFile.getMultipartFileId())).andReturn(multipartFile).anyTimes();
     EasyMock.replay(contractService);
  }
}

您可以将相同的方法应用于JUnit

票数 1
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46292928

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档