I have an file upload endpoint (/document) in a controller defined as follows:@RestControllerpublic class FileUploadController { @Autowired private PersonCSVReaderService personCSVReaderService; @PostMapping(value = "/document", consumes= {MediaType.MULTIPART_FORM_DATA_VALUE}) public void handleFileUpload3(@RequestPart("file") MultipartFile file, @RequestPart("metadata") DocumentMetadata metadata) { System.out.println(String.format("uploading file %s of %s bytes", file.getOriginalFilename(), file.getSize())); personCSVReaderService.readPersonCSV(file, metadata); }}I can test this endpoint using Advanced Rest Client (ARC) or Postman by defining the "file" part referencing the people.csv file and a text part specifying some sample metadata JSON.Everything works fine and I get a 200 status back with the people.csv file contents being written to the console output by the service method: uploading file people.csv of 256 bytes{Address=1, City=2, Date of Birth=6, Name=0, Phone Number=5, State=3, Zipcode=4}Person{name='John Brown', address='123 Main St.', city='Scottsdale', state='AZ', zipcode='85259', phoneNumber='555-1212', dateOfBirth='1965-01-01'}Person{name='Jan Black', address='456 University Dr.', city='Atlanta', state='GA', zipcode='30306', phoneNumber='800-1111', dateOfBirth='1971-02-02'}Person{name='Mary White', address='789 Possum Rd.', city='Nashville', state='TN', zipcode='37204', phoneNumber='888-2222', dateOfBirth='1980-03-03'}Now, I want to run this as an automated Karate test. I have specified a MockConfig :@Configuration@EnableAutoConfiguration@PropertySource("classpath:application.properties")public class MockConfig { // Services ... @Bean public PersonCSVReaderService personCSVReaderService() { return new PersonCSVReaderService(); } // Controllers ... @Bean public FileUploadController fileUploadController() { return new FileUploadController(); }}I also have a MockSpringMvcServlet in the classpath and my karate-config.js is :function fn() { var env = karate.env; // get system property 'karate.env' if (!env) { env = 'dev'; } karate.log('karate.env system property was:', env); var config = { env: env, myVarName: 'someValue', baseUrl: 'http://localhost:8080' } if (env == 'dev') { var Factory = Java.type('MockSpringMvcServlet'); karate.configure('httpClientInstance', Factory.getMock()); //var result = karate.callSingle('classpath:demo/headers/common-noheaders.feature', config); } else if (env == 'stg') { // customize } else if (env == 'prod') { // customize } return config;}Other karate tests run ok using the mock servlet.However, when I try this test to test the /document endpoint:Feature: file upload end-point Background: * url baseUrl * configure lowerCaseResponseHeaders = true Scenario: upload file Given path '/document' And header Content-Type = 'multipart/form-data' And multipart file file = { read: 'people.csv', filename: 'people.csv', contentType: 'text/csv' } And multipart field metadata = { name: "joe", description: "stuff" } When method post Then status 200I get this error:16:14:42.674 [main] INFO com.intuit.karate - karate.env system property was: dev16:14:42.718 [main] INFO o.s.mock.web.MockServletContext - Initializing Spring DispatcherServlet ''16:14:42.719 [main] INFO o.s.web.servlet.DispatcherServlet - Initializing Servlet ''16:14:43.668 [main] INFO o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$a4c7d08f] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)16:14:43.910 [main] INFO o.h.validator.internal.util.Version - HV000001: Hibernate Validator 6.0.14.Final16:14:44.483 [main] INFO o.s.s.c.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor'16:14:44.968 [main] INFO o.s.b.a.e.web.EndpointLinksResolver - Exposing 2 endpoint(s) beneath base path '/actuator'16:14:45.006 [main] INFO o.s.web.servlet.DispatcherServlet - Completed initialization in 2287 ms16:14:45.066 [main] INFO c.i.k.mock.servlet.MockHttpClient - making mock http client request: POST - http://localhost:8080/document16:14:45.085 [main] DEBUG c.i.k.mock.servlet.MockHttpClient -1 > POST http://localhost:8080/document1 > Content-Type: multipart/form-data16:14:45.095 [main] ERROR com.intuit.karate - http request failed: nullfile-upload.feature:13 - nullHTML report: (paste into browser to view) | Karate version: 0.9.2I can only assume that the arguments did not conform to what my endpoint was expecting - I never entered the endpoint in debug mode.I tried this: And multipart file file = read('people.csv') And multipart field metadata = { name: "joe", description: "stuff" }But that was a non-starter as well.What am I doing wrong? The people.csv is in the same folder as fileupload.feature, so I am assuming it is finding the file. I also looked at upload.feature file in the Karate demo project given here:Karate demo project upload.featureBut I could not make it work. Any help appreciated. Thanks in advance.The Postman request looks like this:EDIT UPDATE:I could not get the suggested answer to work.Here is the feature file:Feature: file upload Background: * url baseUrl * configure lowerCaseResponseHeaders = true Scenario: upload file Given path '/document' And header Content-Type = 'multipart/form-data' * def temp = karate.readAsString('people.csv') * print temp And multipart file file = { value: '#(temp)', filename: 'people.csv', contentType: 'text/csv' } And multipart field metadata = { value: {name: 'joe', description: 'stuff'}, contentType: 'application/json' } When method post Then status 200And here is the console output from running that test:09:27:22.051 [main] INFO com.intuit.karate - found scenario at line: 7 - ^upload file$09:27:22.156 [main] INFO com.intuit.karate - karate.env system property was: dev09:27:22.190 [main] INFO o.s.mock.web.MockServletContext - Initializing Spring DispatcherServlet ''09:27:22.190 [main] INFO o.s.web.servlet.DispatcherServlet - Initializing Servlet ''09:27:23.084 [main] INFO o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$a4c7d08f] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)09:27:23.327 [main] INFO o.h.validator.internal.util.Version - HV000001: Hibernate Validator 6.0.14.Final09:27:23.896 [main] INFO o.s.s.c.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor'09:27:24.381 [main] INFO o.s.b.a.e.web.EndpointLinksResolver - Exposing 2 endpoint(s) beneath base path '/actuator'09:27:24.418 [main] INFO o.s.web.servlet.DispatcherServlet - Completed initialization in 2228 ms09:27:24.435 [main] INFO com.intuit.karate - [print] Name,Address,City,State,Zipcode,Phone Number,Date of BirthJohn Brown,123 Main St.,Scottsdale,AZ,85259,555-1212,1965-01-01Jan Black,456 University Dr.,Atlanta,GA,30306,800-1111,1971-02-02Mary White,789 Possum Rd.,Nashville,TN,37204,888-2222,1980-03-0309:27:24.482 [main] INFO c.i.k.mock.servlet.MockHttpClient - making mock http client request: POST - http://localhost:8080/document09:27:24.500 [main] DEBUG c.i.k.mock.servlet.MockHttpClient -1 > POST http://localhost:8080/document1 > Content-Type: multipart/form-data09:27:24.510 [main] ERROR com.intuit.karate - http request failed: nullfile-upload.feature:14 - nullHTML report: (paste into browser to view) | Karate version: 0.9.2Note: people.csv file reads successfully and prints in console. 解决方案 Feature: upload csvBackground: And def admin = read('classpath:com/project/data/adminLogin.json')Scenario:Given url baseUrlAnd header Authorization = admin.tokenAnd multipart file residentDetails = { read:'classpath:com/project/data/ResidentApp_Details.csv', filename: 'ResidentApp_Details.csv' }When method POSTThen status 200Note: Add only one extra line i.e And multipart file residentDetails = { read:'classpath:com/project/data/ResidentApp_Details.csv', filename: 'ResidentApp_Details.csv' } 这篇关于空手道:如何测试多部分表单数据端点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-30 20:05