pls在我的控制器类和服务类下面找到
@RequestMapping(value = "/offers/{jobTitle}/applications", method = RequestMethod.POST, consumes = {
"multipart/form-data" })
public ResponseEntity<Object> uploadMultipartFile(@RequestPart("file") MultipartFile file,
@PathVariable String jobTitle, @RequestParam("applicationStatus") String applicationStatus,
@RequestParam("name") String name, @RequestParam("emailId") String emailId) throws IOException {
Application app = applicationService.createApplicationMultipartFile(file, jobTitle, applicationStatus, name,
emailId);
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{jobTitle}")
.buildAndExpand(app.getOffer().getJobTitle()).toUri();
return ResponseEntity.created(location).body(app);
}
服务类是
@Override
public Application createApplicationMultipartFile(MultipartFile file, String jobTitle, String applicationStatus,
String name, String emailId) throws IOException {
if (!offerRepository.existsById(jobTitle)) {
throw new ResourceNotFoundException("JobTitle " + jobTitle + " not found !!");
}
List<String> emailIds = new ArrayList<>();
List<Application> appliedApplications = applicationRepository.findByOfferJobTitle(jobTitle);
for (Application app : appliedApplications) {
emailIds.add(app.getEmailId());
}
if (emailIds.contains(emailId)) {
throw new ExistingResourceException("User " + emailId + " has already applied for the given Post !!");
}
Offer offer = offerRepository.findById(jobTitle).get();
Application application = new Application();
application.setApplicationStatus(ApplicationStatus.valueOf(applicationStatus));
application.setResume(file.getBytes());
application.setName(name);
application.setEmailId(emailId);
application.setOffer(offer);
return applicationRepository.save(application);
}
我想为控制器编写单元测试用例。我为此使用testNg和Mockito。 以下是我的理解
public class ApplicationControllerTest {
private MockMvc mvc;
private JacksonTester<Application> jsonApplication;
@Mock
ApplicationService appService;
@InjectMocks
ApplicationController appController;
private Offer offer;
private Application app1;
List<Application> appList1;
@BeforeMethod
public void setup() {
offer = new Offer("LSE", new Date(),1);
MockitoAnnotations.initMocks(this);
mvc = MockMvcBuilders.standaloneSetup(appController)
.build();
JacksonTester.initFields(this, new ObjectMapper());
}
@Test
public void canCreateANewApplicationMultiPart() throws Exception {
Mockito.when(appService.createApplicationMultipartFile(Mockito.any(MultipartFile.class), Mockito.eq("LSE"), Mockito.any(String.class), Mockito.any(String.class), Mockito.any(String.class))).thenReturn(app1);
MockHttpServletResponse response = mvc.perform(post("/offers/LSE/applications").contentType(MediaType.MULTIPART_FORM_DATA_VALUE)
.content(jsonApplication.write(new Application("john","john123","res".getBytes(),offer,ApplicationStatus.APPLIED)).getJson())).andReturn().getResponse();
assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED.value());
assertThat(response.getContentAsString()).isEqualTo(new ObjectMapper().writeValueAsString(app1));
}
我猜我的控制器希望在@requestParam中输入内容,这就是为什么我遇到错误。如果可能,请提供此控制器方法的测试用例