一个完整的功能完备的spock demo
class LarkServiceTest extends Specification {
@Spy
LarkAdapter lAdapter;
@Spy
LarkService lService;
@Mock
AopContext aopContext;
@Mock
CustomDataRepositoryV2 dataRepository
RestTemplate restTemplate = restTemplate()
private String appId = "cli_a06";
private String domain = "https://open.feishu.cn/open-apis/";
private String batchUserIdUri = "user/v1/batch_get_id";
void setup() {
ThreadLocalCache.set(ContextConstant.TENANT_CODE, '1000')
ThreadLocalCache.set(ContextConstant.EMPLOYEE_ID, 2012300012)
MockitoAnnotations.initMocks(this)
Mockito.when(dataRepository.load(Matchers.anyString(), Matchers.anyString(), Matchers.anyString()))
.thenAnswer({ args -> mockEntityData(args) })
Whitebox.setInternalState(lAdapter, "restTemplate", restTemplate)
Whitebox.setInternalState(lAdapter, "appId", appId)
Whitebox.setInternalState(lAdapter, "appSecret", appSecret)
Whitebox.setInternalState(lAdapter, "domain", domain)
Whitebox.setInternalState(lAdapter, "batchUserIdUri", batchUserIdUri)
Whitebox.setInternalState(larkService, "dataRepository", dataRepository)
Whitebox.setInternalState(larkService, "larkAdapter", larkAdapter)
}
void cleanup() {
}
def "exchangeEmailForIDTest"() {
when:
Map map = lAdapter.exchangeEmailsForOpenIds(Arrays.asList("test@mail.com"))
then:
map != null && !map.isEmpty()
}
def "CreateGroupWithWrongEmployee"() {
when:
ThreadLocalCache.set(ContextConstant.EMPLOYEE_ID, 1000000L)
String str = lService.createGroup("test1", "111")
then:
def e = thrown(Exception)
e.message != ""
}
def "CreateGroupTest"() {
when:
Pair pair = Pair.of("2012300012", Arrays.asList("2012300012"))
Mockito.doReturn(pair).when(lService).getLarkMemberPair(Mockito.any())
Mockito.doReturn("test").when(lAdapter).createLarkGroup(Mockito.any(), Mockito.any(), Mockito.any())
String str = larkService.createGroup("test0", "111")
then:
def e = thrown(Exception)
e.message != ""
}
private RestTemplate restTemplate() {
OkHttp3ClientHttpRequestFactory okHttpFactory = new OkHttp3ClientHttpRequestFactory();
okHttpFactory.setConnectTimeout(10000);
okHttpFactory.setReadTimeout(10000);
RestTemplate restTemplate = new RestTemplate(okHttpFactory);
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
List<MediaType> mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.TEXT_PLAIN);
mediaTypes.add(MediaType.TEXT_HTML);
converter.setSupportedMediaTypes(mediaTypes);
restTemplate.getMessageConverters().add(converter);
return restTemplate;
}
.thenAnswer({ args -> mockLoadEntityData(args) })
EntityData mockLoadEntityData(InvocationOnMock args) {
args.getArgumentAt(0, String.class)
def entityName = args.getArgumentAt(1, String.class)
EntityData result = null
if (entityName == 'test0') {
result = [
primaryKey : '111',
tenantCode : RequestContext.tenantCode,
sysName : 'test',
recordType : 'rec',
entityName : entityName,
fieldList : [
[
fieldName : 'primaryName',
fieldValue: 'primaryName',
action : ActionEnum.NONE,
fieldMeta : [
fieldName : 'primaryName',
type : FieldTemplateTypeEnum.TYPE_STRING,
valueType : FieldValueTypeEnum.TYPE_STRING,
] as EntityField,
] as OthersField,
[
fieldName : 'ownerId',
fieldMeta : [
fieldName : 'Lark_Group',
valueType : FieldValueTypeEnum.TYPE_STRING,
type : FieldTemplateTypeEnum.TYPE_STRING,
relationName: []
] as EntityField,
fieldValue: '8011869',
action : ActionEnum.NONE,
] as OthersField,
] as List<CustomField>,
entityAction: ActionEnum.NONE,
] as EntityData
} else if (entityName == 'test3') {
result = [
primaryKey : '111',
tenantCode : RequestContext.tenantCode,
sysName : 'test',
recordType : 'rec',
entityName : entityName,
fieldList : [
[
fieldName : 'ownerId',
fieldMeta : [
fieldName : 'Lark_Group',
valueType : FieldValueTypeEnum.TYPE_STRING,
type : FieldTemplateTypeEnum.TYPE_STRING,
relationName: []
] as EntityField,
fieldValue: '',
action : ActionEnum.NONE,
] as OthersField
] as List<CustomField>,
entityAction: ActionEnum.NONE,
] as EntityData
}
return result
}
}
adapter源码:
public class LarkAdapter {
@Value("${open.app_id:cli_9f28c167c635900c}")
private String appId;
@Value("${open.app_secret:jDdumhMXPh9FInntZ2hb4bPGLOEiyED4}")
private String appSecret;
@Value("${open.domain:https:/……}")
private String domain;
private RestTemplate restTemplate = restTemplate();
……
}
|