alibaba / alibaba/testable-mock

MockMethod注解无法生效

Open
#185 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
1.8k
Forks
306
PR merge metrics
No merged PRs in 30d

Description

无法mock,在调用AgentIServiceImpl类的 getAvailableAgentCount方法时,agentMapper一直报空指针的错。
测试类:

public class AgentIServiceImplTest {

private AgentIServiceImpl agentIService = new AgentIServiceImpl();

public static class Mock {
@MockMethod(targetClass = AgentIServiceImpl.class)
public int getAvailableCount() {
System.out.println("mock生效");
return 1;
}

@MockMethod(targetClass = AgentIServiceImpl.class)
public Map getAvailableRegionCount() {
System.out.println("mock生效");
return new HashMap<>();
}
}

@Test
public void testGetAvailableCount() {
Map res = agentIService.getAvailableRegionCount();
assertNotNull(res);
//int count = agentIService.getAvailableAgentCount();
//assertEquals(1, count);
}
}

源类:
public class AgentIServiceImpl extends ServiceImpl implements AgentIService {

@Resource
private AgentMapper agentMapper;

@Override
public int getAvailableCount() {
return agentMapper.getAvailableAgentCount();
}

@Override
public Map getAvailableRegionCount() {
return agentMapper.queryAgentRegionCount();
}

}

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with AgentIServiceImplTest and the AgentIServiceImpl methods getAvailableCount and getAvailableRegionCount, then inspect how @MockMethod is discovered and applied. Run testGetAvailableCount and compare the annotated Mock methods with the invoked service methods; done means the mock is applied and the test no longer fails because agentMapper is null.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.