如何使用 Mockito 模拟内部 class 实例
How to mock Inner class instance using Mockito
@ConfigurationProperties(prefix= 'app')
@Getter @Setter
public class AppConfig{
private ExternalService externalService=new ExternalService();
@Getter @Setter
public static class ExternalService{
private String url;
private String authToken;
}
}
我正在使用 AppConfig 的服务。
@Service
@AllArgsConstructor
public class ExternalService{
private final AppConfig appConfig;
public boolean isAuthorize(String token){
String authUrl=appConfig.getExternalService().getUrl();
boolean isAuthorize= //External Apis call
return isAuthorize;
}
}
测试 class 外部服务
@ExtendWith(MockitoExtension.class)
class ExternalTestService{
@Mock
private AppConfig AppConfig;
@Mock
private AppConfig.ExternalService externalSeriveConfig;
@InjectMocks
private ExternalService externalService;
@Test
public void shouldAuthorize(){
//Null Pointer exception for AppConfig.getExternalService()
Mockito.when(AppConfig.getExternalService().getUrl()).thenReturn("123456");
Assertions.assertEquals(externalService.isAuthorize(),true);
}
如果我在 shouldAuthorize 中模拟 GradingProperties.CentralServiceConfig 那么它工作正常但是在 Assertions.assertEquals at ExternalService(String authUrl=appConfig.getExternalService().getUrl();) 时出现 NullPointerException
喜欢
@Test
public void shouldAuthorize(){
AppConfig.ExternalService externalMock=Mockito.mock(AppConfig.ExternalService.class);
Mockito.when(externalMock.getUrl()).thenReturn("123456");
Assertions.assertEquals(externalService.isAuthorize(),true);
}
如何模拟并使此代码可运行
当你有链式方法调用时,你需要确保链式调用的每个部分returns非空结果。
Mockito.when(AppConfig.getExternalService().getUrl()).thenReturn("123456");
您还没有对 AppConfig
进行任何调用,因此 AppConfig.getExternalService()
returns 无效。
你需要:
Mockito.when(AppConfig.getExternalService()).thenReturn(externalSeriveConfig);
Mockito.when(AppConfig.getExternalService().getUrl()).thenReturn("123456");
或者,甚至更好:
Mockito.when(AppConfig.getExternalService()).thenReturn(externalSeriveConfig);
Mockito.when(externalSeriveConfig.getUrl()).thenReturn("123456");
@ConfigurationProperties(prefix= 'app')
@Getter @Setter
public class AppConfig{
private ExternalService externalService=new ExternalService();
@Getter @Setter
public static class ExternalService{
private String url;
private String authToken;
}
}
我正在使用 AppConfig 的服务。
@Service
@AllArgsConstructor
public class ExternalService{
private final AppConfig appConfig;
public boolean isAuthorize(String token){
String authUrl=appConfig.getExternalService().getUrl();
boolean isAuthorize= //External Apis call
return isAuthorize;
}
}
测试 class 外部服务
@ExtendWith(MockitoExtension.class)
class ExternalTestService{
@Mock
private AppConfig AppConfig;
@Mock
private AppConfig.ExternalService externalSeriveConfig;
@InjectMocks
private ExternalService externalService;
@Test
public void shouldAuthorize(){
//Null Pointer exception for AppConfig.getExternalService()
Mockito.when(AppConfig.getExternalService().getUrl()).thenReturn("123456");
Assertions.assertEquals(externalService.isAuthorize(),true);
}
如果我在 shouldAuthorize 中模拟 GradingProperties.CentralServiceConfig 那么它工作正常但是在 Assertions.assertEquals at ExternalService(String authUrl=appConfig.getExternalService().getUrl();) 时出现 NullPointerException 喜欢
@Test
public void shouldAuthorize(){
AppConfig.ExternalService externalMock=Mockito.mock(AppConfig.ExternalService.class);
Mockito.when(externalMock.getUrl()).thenReturn("123456");
Assertions.assertEquals(externalService.isAuthorize(),true);
}
如何模拟并使此代码可运行
当你有链式方法调用时,你需要确保链式调用的每个部分returns非空结果。
Mockito.when(AppConfig.getExternalService().getUrl()).thenReturn("123456");
您还没有对 AppConfig
进行任何调用,因此 AppConfig.getExternalService()
returns 无效。
你需要:
Mockito.when(AppConfig.getExternalService()).thenReturn(externalSeriveConfig);
Mockito.when(AppConfig.getExternalService().getUrl()).thenReturn("123456");
或者,甚至更好:
Mockito.when(AppConfig.getExternalService()).thenReturn(externalSeriveConfig);
Mockito.when(externalSeriveConfig.getUrl()).thenReturn("123456");