refactor: 单元测试方法生成类
parent
022d7c6bf0
commit
ae0085e965
File diff suppressed because it is too large
Load Diff
|
|
@ -3,82 +3,147 @@ package com.czcb.scfs.api.test;
|
|||
import lombok.experimental.UtilityClass;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
@UtilityClass
|
||||
public class RefGenTestMethod {
|
||||
private static final String FMT_TEST = "@Test \nvoid %sTest() {\n";
|
||||
private static final String FMT_REQUEST = " %s request = new %s();\n";
|
||||
private static final String FMT_REQUEST_JSON = " String requestJson = Json.toJson(request);\n Assertions.assertEquals(\"\", requestJson);\n";
|
||||
private static final String FMT_RESPONSE = " %s response = new %s();\n";
|
||||
private static final String FMT_RESPONSE_JSON = " String responseJson = Json.toJson(response);\n Assertions.assertEquals(\"\", responseJson);";
|
||||
private static final String FMT_RESPONSE_RESULT_JSON = " String resultJson = Json.toJson(result);\n Assertions.assertEquals(\"\", resultJson);";
|
||||
private static final String FMT_REQUEST_METHOD = " request.%s(\"%s\");\n";
|
||||
private static final String FMT_RESPONSE_METHOD = " response.%s(\"%s\");\n";
|
||||
private static final String FMT_REQUEST_ASSERTIONS = " Assertions.assertEquals(\"%s\", request.%s());\n";
|
||||
private static final String FMT_RESPONSE_ASSERTIONS = " Assertions.assertEquals(\"%s\", response.%s());\n";
|
||||
private static final String FMT_REQUEST_MOCK = " HttpResponse<%s> httpResponse = TestHttpResponse.create(\"%s\", response, %s.class);\n";
|
||||
private static final String FMT_REQUEST_MOCK_REST = " %s result = service.%s(request);\n";
|
||||
private static final String FMT_REQUEST_MOCK_ITO = " Mockito.when(apiClient.post(\n" +
|
||||
private static final String FMT_REQUEST = "\t%s request = new %s();\n";
|
||||
private static final String FMT_REQUEST_JSON = "\tString requestJson = Json.toJson(request);\n\tAssertions.assertEquals(\"\", requestJson);\n\n";
|
||||
private static final String FMT_RESPONSE = "\t%s response = new %s();\n";
|
||||
private static final String FMT_RESPONSE_JSON = "\tString responseJson = Json.toJson(response);\n\tAssertions.assertEquals(\"\", responseJson);\n\n";
|
||||
private static final String FMT_RESPONSE_RESULT_JSON = "\tString resultJson = Json.toJson(result);\n\tAssertions.assertEquals(\"\", resultJson);\n";
|
||||
private static final String FMT_REQUEST_METHOD = "\trequest.%s(\"%s\");\n";
|
||||
private static final String FMT_RESPONSE_METHOD = "\tresponse.%s(\"%s\");\n";
|
||||
private static final String FMT_REQUEST_ASSERTIONS = "\tAssertions.assertEquals(\"%s\", request.%s());\n";
|
||||
private static final String FMT_NOT_NULL_ASSERTIONS = "\tAssertions.assertNotNull(%s.%s());\n";
|
||||
private static final String FMT_RESPONSE_ASSERTIONS = "\tAssertions.assertEquals(\"%s\", response.%s());\n";
|
||||
private static final String FMT_REQUEST_MOCK = "\tHttpResponse<%s> httpResponse = TestHttpResponse.create(\"%s\", response, %s.class);\n";
|
||||
private static final String FMT_REQUEST_MOCK_REST = "\t%s result = service.%s(request);\n\n";
|
||||
private static final String FMT_REQUEST_MOCK_ITO = "\tMockito.when(apiClient.post(\n" +
|
||||
" Mockito.eq(\"%s\"),\n" +
|
||||
" Mockito.any(HttpHeaders.class),\n" +
|
||||
" Mockito.any(RequestBody.class),\n" +
|
||||
" Mockito.eq(%s.class)))\n" +
|
||||
" .thenReturn(httpResponse);\n";
|
||||
" .thenReturn(httpResponse);\n\n";
|
||||
|
||||
public static void gen(Class<?> request, Class<?> response, String methodName, String url) {
|
||||
System.out.printf(FMT_TEST, methodName);
|
||||
System.out.printf(FMT_REQUEST, request.getSimpleName(), request.getSimpleName());
|
||||
private static final String EXTRACTED_REQUEST = "\textracted%sParams(request);\n";
|
||||
private static final String EXTRACTED_REQUEST_ASSERTIONS = "\textracted%sAssertions(request);\n\n";
|
||||
private static final String EXTRACTED_RESPONSE = "\textracted%sParams(response);\n";
|
||||
private static final String EXTRACTED_RESPONSE_ASSERTIONS = "\textracted%sAssertions(response);\n\n";
|
||||
|
||||
public static void genRequestParams(Class<?> request) {
|
||||
print("void extracted%sParams(%s request) {\n", request.getSimpleName(), request.getSimpleName());
|
||||
|
||||
Method[] methods = request.getMethods();
|
||||
for (Method method : methods) {
|
||||
if (!method.getName().startsWith("set")) {
|
||||
continue;
|
||||
}
|
||||
System.out.printf(FMT_REQUEST_METHOD, method.getName(), method.getName());
|
||||
|
||||
printParams(FMT_REQUEST_METHOD, method);
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
print("}\n");
|
||||
}
|
||||
|
||||
public static void genRequestAssertions(Class<?> request) {
|
||||
print("void extracted%sAssertions(%s request) {\n", request.getSimpleName(), request.getSimpleName());
|
||||
|
||||
Method[] methods = request.getMethods();
|
||||
for (Method method : methods) {
|
||||
if (!method.getName().startsWith("set")) {
|
||||
continue;
|
||||
}
|
||||
System.out.printf(FMT_REQUEST_ASSERTIONS, method.getName(), method.getName().replace("set", "get"));
|
||||
|
||||
printAssertions(FMT_REQUEST_ASSERTIONS, method, true);
|
||||
}
|
||||
print("}\n");
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
System.out.println("System.out.println(Json.toJson(request));");
|
||||
System.out.printf(FMT_REQUEST_JSON);
|
||||
System.out.println();
|
||||
public static void genResponseParams(Class<?> response) {
|
||||
print("void extracted%sParams(%s response) {\n", response.getSimpleName(), response.getSimpleName());
|
||||
|
||||
System.out.printf(FMT_RESPONSE, response.getSimpleName(), response.getSimpleName());
|
||||
Method[] responseMethods = response.getMethods();
|
||||
for (Method method : responseMethods) {
|
||||
Method[] methods = response.getMethods();
|
||||
for (Method method : methods) {
|
||||
if (!method.getName().startsWith("set")) {
|
||||
continue;
|
||||
}
|
||||
System.out.printf(FMT_RESPONSE_METHOD, method.getName(), method.getName());
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
for (Method method : responseMethods) {
|
||||
printParams(FMT_RESPONSE_METHOD, method);
|
||||
}
|
||||
print("}\n");
|
||||
}
|
||||
|
||||
public static void genResponseAssertions(Class<?> response) {
|
||||
print("void extracted%sAssertions(%s response) {\n", response.getSimpleName(), response.getSimpleName());
|
||||
|
||||
Method[] methods = response.getMethods();
|
||||
for (Method method : methods) {
|
||||
if (!method.getName().startsWith("set")) {
|
||||
continue;
|
||||
}
|
||||
System.out.printf(FMT_RESPONSE_ASSERTIONS, method.getName(), method.getName().replace("set", "get"));
|
||||
|
||||
printAssertions(FMT_RESPONSE_ASSERTIONS, method, false);
|
||||
}
|
||||
print("}\n");
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
System.out.println("System.out.println(Json.toJson(response));");
|
||||
System.out.printf(FMT_RESPONSE_JSON);
|
||||
System.out.println("\n");
|
||||
public static void gen(Class<?> request, Class<?> response, String methodName, String url) {
|
||||
print(FMT_TEST, methodName);
|
||||
print(FMT_REQUEST, request.getSimpleName(), request.getSimpleName());
|
||||
|
||||
System.out.printf(FMT_REQUEST_MOCK, response.getSimpleName(), url, response.getSimpleName());
|
||||
System.out.printf(FMT_REQUEST_MOCK_ITO, url, response.getSimpleName());
|
||||
System.out.printf(FMT_REQUEST_MOCK_REST, response.getSimpleName(), methodName);
|
||||
print(EXTRACTED_REQUEST, request.getSimpleName());
|
||||
print(EXTRACTED_REQUEST_ASSERTIONS, request.getSimpleName());
|
||||
|
||||
System.out.println();
|
||||
System.out.println("System.out.println(Json.toJson(result));");
|
||||
System.out.printf(FMT_RESPONSE_RESULT_JSON);
|
||||
System.out.println("\n}");
|
||||
print("\tSystem.out.println(Json.toJson(request));\n");
|
||||
print(FMT_REQUEST_JSON);
|
||||
|
||||
print(FMT_RESPONSE, response.getSimpleName(), response.getSimpleName());
|
||||
print(EXTRACTED_RESPONSE, response.getSimpleName());
|
||||
print(EXTRACTED_RESPONSE_ASSERTIONS, response.getSimpleName());
|
||||
|
||||
print("\tSystem.out.println(Json.toJson(response));\n");
|
||||
print(FMT_RESPONSE_JSON);
|
||||
|
||||
print(FMT_REQUEST_MOCK, response.getSimpleName(), url, response.getSimpleName());
|
||||
print(FMT_REQUEST_MOCK_ITO, url, response.getSimpleName());
|
||||
print(FMT_REQUEST_MOCK_REST, response.getSimpleName(), methodName);
|
||||
|
||||
print("\tSystem.out.println(Json.toJson(result));\n");
|
||||
print(FMT_RESPONSE_RESULT_JSON);
|
||||
print("}\n");
|
||||
|
||||
genRequestParams(request);
|
||||
genRequestAssertions(request);
|
||||
genResponseParams(response);
|
||||
genResponseAssertions(response);
|
||||
}
|
||||
|
||||
private static void print(String format, Object... args) {
|
||||
System.out.printf(format, args);
|
||||
}
|
||||
|
||||
private static void printParams(String fmt, Method method) {
|
||||
if (method.getParameterTypes()[0].isAssignableFrom(String.class)) {
|
||||
print(fmt, method.getName(), method.getName());
|
||||
} else if (method.getParameterTypes()[0].isAssignableFrom(BigDecimal.class)) {
|
||||
print(String.format(fmt, method.getName(), "BigDecimal.valueOf(100.00)").replace("\"", ""));
|
||||
} else if (method.getParameterTypes()[0].isAssignableFrom(List.class)) {
|
||||
print(String.format(fmt, method.getName(), "Lists.list()").replace("\"", ""));
|
||||
}
|
||||
}
|
||||
|
||||
private static void printAssertions(String fmt, Method method, boolean isRequest) {
|
||||
if (method.getParameterTypes()[0].isAssignableFrom(String.class)) {
|
||||
print(fmt, method.getName(), method.getName().replace("set", "get"));
|
||||
} else if (method.getParameterTypes()[0].isAssignableFrom(BigDecimal.class)) {
|
||||
print(String.format(fmt, "BigDecimal.valueOf(100.00)", method.getName().replace("set", "get"))
|
||||
.replace("\"", ""));
|
||||
} else if (method.getParameterTypes()[0].isAssignableFrom(List.class)) {
|
||||
print(String.format(FMT_NOT_NULL_ASSERTIONS, isRequest ? "request" : "response", method.getName()
|
||||
.replace("set", "get")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue