我是Spring和Mockito测试的新手。我在Stack Overflow上找不到问题的答案。
我有以下课程:
@Component
@Chronolog(logLevel = ChronologLevel.DEBUG)
public class CityService {
private static final Logger LOGGER = LoggerFactory.getLogger(CityService.class);
@Autowired
private GeoClient geoClient;
@Autowired
private EOClient eoClient;
/**
*
* @param rrCode
* @return
*/
public City getCityFromCode(String rrCode) {
City city = new City();
LinkedHashMap geoResponse = geoClient.getCityInfoByRRCode(rrCode);
city.setRrCode(rrCode);
LinkedHashMap result = (LinkedHashMap) geoResponse.get("City");
if(result != null){
city.setLabel((String) result.get("Name"));
}else{
city.setLabel("");
}
return city;
}
/**
*
* @param maxVia
* @return
*/
public List<OrigineDestination> getODFromMaxVia(String maxVia) {
List<OrigineDestination> originesDestinations = new ArrayList<OrigineDestination>();
OrigineDestination origineDestination;
Trips odResponse = eoClient.getCityInfoByMAxVia(maxVia);
for (Trip trip : odResponse.getTrip()) {
origineDestination = new OrigineDestination();
origineDestination.setOriginCity(trip.getOriginCity());
origineDestination.setOriginStation(trip.getOriginStation());
origineDestination.setDestinationCity(trip.getDestinationCity());
origineDestination.setDestinationStation(trip.getDestinationStation());
originesDestinations.add(origineDestination);
}
return originesDestinations;
}
/**
*
* @return
*/
public Set<City> getOrigins() {
List<OrigineDestination> retourOriginesDestination = getODFromMaxVia("1");
Set<City> origins = new HashSet<>();
Set<String> orrCodes = new HashSet<>();
for (OrigineDestination origineDestination : retourOriginesDestination) {
if (!orrCodes.contains(origineDestination.getOriginCity())) {
orrCodes.add(origineDestination.getOriginCity());
City city = new City();
city.setRrCode(origineDestination.getOriginCity());
city.setLabel(getCityFromCode(city.getRrCode()).getLabel());
if(!city.getLabel().isEmpty()){
origins.add(city);
}
}
}
return origins;
}
/**
*
* @param origin
* @return
*/
public Set<City> getDestinations(String origin) {
List<OrigineDestination> retourOriginesDestination = getODFromMaxVia("1");
Set<City> matchedOrigins = new HashSet<>();
Set<String> drrCodes = new HashSet<>();
for (OrigineDestination origineDestination : retourOriginesDestination) {
if (origin == null ||
(origin != null && origin.toUpperCase().equals(origineDestination.getOriginCity()))) {
if (!drrCodes.contains(origineDestination.getDestinationCity())) {
drrCodes.add(origineDestination.getDestinationCity());
City city = new City();
city.setRrCode(origineDestination.getDestinationCity());
city.setLabel(getCityFromCode(city.getRrCode()).getLabel());
if(!city.getLabel().isEmpty()){
matchedOrigins.add(city);
}
}
}
}
return matchedOrigins;
}
}
我的测试类看起来像:
@RunWith(MockitoJUnitRunner.class)
public class CityServiceTest {
@InjectMocks
private CityService cityService;
@Mock
GeoClient geoClient;
@Mock
EOClient eoClient;
@Test
public void testGetOriginsCasOK() {
cityService.getOrigins();
}
@Test
public void testGetDestinationsCasOK() {
cityService.getDestinations("FRPAR");
}
@Test
public void testGetDestinationsCasKO() {
cityService.getDestinations("FRET");
}
}
我的问题是服务为空我不想嘲笑它。我尝试了几个注释,但没有找到使用哪个注释。
我做错了什么?
java.lang.NullPointerException
at com.xxxx.matrixar.services.CityService.getODFromMaxVia(CityService.java:89)
at com.xxxx.matrixar.services.CityService.getDestinations(CityService.java:129)
at com.xxxx.matrixar.services.CityServiceTest.testGetDestinationsCasKO(CityServiceTest.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
答案 0 :(得分:1)
您需要指定模拟对象在实际调用时应返回的内容。
例如,在CityService.getODFromMaxVia()
中,您调用geoClient.getCityInfoByRRCode()
,然后使用此调用返回的结果。
除非您指定其他内容,否则模拟将始终返回null
:
when( geoClient.getCityInfoByRRCode( "FRPAR" ) ).thenReturn( ... );
这允许您绕过模拟对象中的复杂代码,并返回您在这种情况下期望的内容。
请记住:您在此处测试服务,而不是地理客户端。在其他地方有地理客户端的测试,所以没有必要确保它在服务中使用时做正确的事。