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

Vis*_*ukk 6 java junit spring mockito

我有以下服务:

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    protected ContractService contractService;

    private void saveInCache(MultipartFile multipartFile) {
        this.contractService.saveInCache(multipartFile);
    }
}
Run Code Online (Sandbox Code Playgroud)

和另一项服务

@Service
public class ClientServiceImpl implements ClientService {

    @Autowired
    protected ContractService contractService;

    private void getInfoOfFile(String multipartFileId) {
        DocumentInfo document = this.contractService.getInfo(multipartFileId);
        ///
    }
}
Run Code Online (Sandbox Code Playgroud)

我有我的 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.
  }
}
Run Code Online (Sandbox Code Playgroud)

当我在调试模式下运行此测试时,我看到它this.contractService.getInfo(multipartFileId);正在返回“null”。

我在嘲笑中哪里出错了。

我刚刚在我的 JUnit 中嘲笑了 ContractService。我是否还需要模拟 AccountServiceImpl ?

编辑:添加 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();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

rob*_*ins 4

我认为您与 clientService 的声明相矛盾。

你有:

@Autowired
@InjectMocks
protected ClientService clientService = new ClientServiceImpl();
Run Code Online (Sandbox Code Playgroud)

这应该创建一个名为 clientService 的自动装配 ClientService 并注入模拟。然而,= new ClientServiceImpl()随后将覆盖自动装配并为您创建一个普通的香草(我认为!)。另外@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);

  }
}
Run Code Online (Sandbox Code Playgroud)

添加@RunWith(MockitoJUnitRunner.class)意味着所有对象的创建都会发生,无需您进行任何进一步的工作。