Created
June 2, 2021 05:22
-
-
Save farazdurrani/c8152b28c9b4104333b86db5ef1bb44a to your computer and use it in GitHub Desktop.
Tests for upload file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bezkoder.spring.files.csv; | |
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; | |
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | |
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | |
import java.nio.charset.StandardCharsets; | |
import org.junit.jupiter.api.BeforeEach; | |
import org.junit.jupiter.api.Test; | |
//import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.mockito.InjectMocks; | |
import org.mockito.Mock; | |
import org.mockito.Mockito; | |
import org.mockito.MockitoAnnotations; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.Primary; | |
import org.springframework.mock.web.MockMultipartFile; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
import org.springframework.test.web.servlet.MockMvc; | |
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | |
import com.bezkoder.spring.files.csv.controller.CSVController; | |
import com.bezkoder.spring.files.csv.service.CSVService; | |
@RunWith(SpringJUnit4ClassRunner.class) | |
public class SpringBootUploadCsvFilesApplicationTests { | |
private MockMvc mockMvc; | |
@Configuration | |
static class Config { | |
@Bean | |
@Primary | |
public CSVService fileServiceMock() { | |
return Mockito.mock(CSVService.class); | |
} | |
} | |
@Mock | |
private CSVService service; | |
@InjectMocks | |
private CSVController controller; | |
@BeforeEach | |
public void setup() { | |
MockitoAnnotations.initMocks(this); | |
this.mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); | |
} | |
@Test | |
void testUploadFile() throws Exception { | |
String content = "Id,Title,Description,Published\n" + "1,Spring Boot Tut#1,Tut#1 Description,FALSE"; | |
byte[] fileContent = content.getBytes(StandardCharsets.UTF_8); | |
MockMultipartFile filePart = new MockMultipartFile("file", "orig.csv", "text/csv", fileContent); | |
this.mockMvc.perform(multipart("/api/csv/upload").file(filePart)).andExpect(status().isOk()) | |
.andExpect(content().contentType("application/json")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment