gpt4 book ai didi

java - 使用mockito测试服务

转载 作者:行者123 更新时间:2023-12-01 12:06:33 26 4
gpt4 key购买 nike

我是 Spring 和 Mockito 测试的新手。我在 Stack Overflow 上找不到问题的答案。

我有以下类(class):

@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");
}
}

我的问题是服务为空,我不想 mock 它。我尝试了几种注释,但没有找到该使用哪一种。

我做错了什么?

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)

最佳答案

您需要指定模拟对象在实际调用时应返回的内容。

例如,在 CityService.getODFromMaxVia() 中,您调用 geoClient.getCityInfoByRRCode(),然后使用此调用返回的结果。

除非您指定其他内容,否则模拟将始终返回null:

when( geoClient.getCityInfoByRRCode( "FRPAR" ) ).thenReturn( ... );

这允许您绕过模拟对象中的复杂代码,并仅返回您在本例中期望的内容。

请记住:您在这里测试您的服务,而不是地理客户端。其他地方已经对地理客户端进行了测试,因此没有必要确保它在服务中使用时执行正确的操作。

关于java - 使用mockito测试服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27548390/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com