use of org.springframework.test.context.web.WebAppConfiguration in project judge by zjnu-acm.
the class MockGenerator method test.
@Test
public void test() throws IOException {
Gson gson = new Gson();
Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
Map<Class<?>, List<Info>> map = handlerMethods.entrySet().stream().map(entry -> new Info(entry.getValue(), entry.getKey())).collect(Collectors.groupingBy(info -> info.getHandlerMethod().getMethod().getDeclaringClass()));
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw, true);
for (Map.Entry<Class<?>, List<Info>> entry : map.entrySet()) {
Class<?> key = entry.getKey();
List<Info> value = entry.getValue();
if (!accept(key, value)) {
continue;
}
value.sort(Comparator.comparing(info -> info.getHandlerMethod().getMethod(), Comparator.comparing(method -> method.getDeclaringClass().getName().replace(".", "/") + "." + method.getName() + ":" + org.springframework.asm.Type.getMethodDescriptor(method), toMethodComparator(key))));
TestClass testClass = new TestClass(key, "@AutoConfigureMockMvc", "@RunWith(SpringRunner.class)", "@Slf4j", "@SpringBootTest(classes = " + mainClass.getSimpleName() + ".class)", "@Transactional", "@WebAppConfiguration");
testClass.addImport(mainClass);
testClass.addImport(AutoConfigureMockMvc.class);
testClass.addImport(RunWith.class);
testClass.addImport(SpringRunner.class);
testClass.addImport(Slf4j.class);
testClass.addImport(SpringBootTest.class);
testClass.addImport(Transactional.class);
testClass.addImport(WebAppConfiguration.class);
testClass.addImport(Autowired.class);
testClass.addField(MockMvc.class, "mvc", "@Autowired");
for (Info info : value) {
RequestMappingInfo requestMappingInfo = info.getRequestMappingInfo();
Set<RequestMethod> requestMethods = requestMappingInfo.getMethodsCondition().getMethods();
String requestMethod = requestMethods.isEmpty() ? "post" : requestMethods.iterator().next().toString().toLowerCase();
HandlerMethod handlerMethod = info.getHandlerMethod();
String url = gson.toJson(requestMappingInfo.getPatternsCondition().getPatterns().iterator().next());
generate(key, requestMappingInfo, handlerMethod, url, testClass, requestMethod);
}
testClass.write(out);
Path to = outputDir.resolve(key.getName().replace(".", "/") + "Test.java");
Files.createDirectories(to.getParent());
Files.write(to, sw.toString().replace("\t", " ").getBytes(StandardCharsets.UTF_8));
sw.getBuffer().setLength(0);
}
}
Aggregations