прошло несколько лет с тех пор, как я последний раз работал с Spring с Spring \ Spring Boot, и у меня возникла следующая проблема.
У меня есть проект Spring Boot, в котором некоторые API-интерфейсы этого класса называются ExcelResource :
package com.springboot.excelapi.resources;
import com.springboot.excelapi.dto.DemoDTO;
import com.springboot.excelapi.dto.ExampleDTO;
import com.springboot.excelapi.services.ExcelService;
import org.springframework.context.annotation.Description;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
@Description(value = "Resource layer for handling REST requests.")
@RestController
@RequestMapping("api")
public class ExcelResource {
private ExcelService excelService;
/**
* Constructor / dependency injector
* @param excelService - service layer dependency.
*/
public ExcelResource(ExcelService excelService) {
this.excelService = excelService;
}
@GetMapping(value = "/known-cells", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<ExampleDTO>> mapExcelRowsToObject() throws IOException
{
List<ExampleDTO> exampleDTOList = this.excelService.readFromExcelWithKnownObject();
return new ResponseEntity<>(exampleDTOList, HttpStatus.OK);
}
@GetMapping(value = "/specific-cells", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<DemoDTO>> mapSpecificCellsToObject() throws IOException
{
List<DemoDTO> demoDTOList = this.excelService.readSpecificCellsFromExcel();
return new ResponseEntity<>(demoDTOList, HttpStatus.OK);
}
@GetMapping(value = "/comp_vibr_and_temp_tab", produces = MediaType.APPLICATION_JSON_VALUE)
public String processCompVibrAndTempoExcelTab() throws IOException
{
//List<DemoDTO> demoDTOList = this.excelService.processVibrationMonitoringExcelTab();
String result = this.excelService.processVibrationMonitoringExcelTab();
return result;
}
}
Хорошо, у меня есть другой класс JUNIT , тестирующий предыдущий API, представленный моим проектом Spring Boot, этот:
package com.springboot.excelapi.integration;
import com.springboot.excelapi.Application;
import com.springboot.excelapi.dto.DemoDTO;
import com.springboot.excelapi.dto.ExampleDTO;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import java.util.List;
import static org.assertj.core.api.Java6Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { Application.class })
@WebAppConfiguration
@ActiveProfiles(profiles = { "no-liquibase" })
public class ExcelResourceIntegrationTest {
private static final String TARGET_API_KNOWN_CELLS = "http://localhost:8080/api/known-cells";
private static final String TARGET_API_SPECIFIC_CELLS = "http://localhost:8080/api/specific-cells";
private static final String TARGET_API_COMP_VIBR_AND_TEMP_MONITORING_TAB = "http://localhost:8080/api/comp_vibr_and_temp_tab";
private TestRestTemplate testRestTemplate;
@Before
public void init() {
testRestTemplate = new TestRestTemplate();
}
@Test
public void mapRowsFromExcelFileTestWithKnownCells()
{
HttpEntity requestEntity = new HttpEntity<>(Void.class);
ParameterizedTypeReference<List<ExampleDTO>> typeRef = new ParameterizedTypeReference<List<ExampleDTO>>() {};
ResponseEntity<List<ExampleDTO>> responseEntity = testRestTemplate.exchange(
TARGET_API_KNOWN_CELLS,
HttpMethod.GET,
requestEntity,
typeRef
);
assertThat(responseEntity.getStatusCodeValue()).isEqualTo(200);
List<ExampleDTO> responseBody = responseEntity.getBody();
assertThat(responseBody.size()).isGreaterThan(0);
assertThat(responseBody.size()).isEqualTo(3);
assertThat(responseBody.get(0).getFullName()).isEqualTo("Heril Muratovic");
}
@Test
public void mapSpecificRows()
{
HttpEntity requestEntity = new HttpEntity<>(Void.class);
ParameterizedTypeReference<List<DemoDTO>> typeReference = new ParameterizedTypeReference<List<DemoDTO>>() {};
ResponseEntity<List<DemoDTO>> responseEntity = testRestTemplate.exchange(
TARGET_API_SPECIFIC_CELLS,
HttpMethod.GET,
requestEntity,
typeReference
);
assertThat(responseEntity.getStatusCodeValue()).isEqualTo(200);
List<DemoDTO> responseBody = responseEntity.getBody();
assertThat(responseBody.size()).isGreaterThan(0);
assertThat(responseBody.size()).isEqualTo(2);
assertThat(responseBody.get(0).getName()).isEqualTo("John Doe");
}
@Test
public void processCompVibrAndTempoExcelTabTest()
{
HttpEntity requestEntity = new HttpEntity<>(Void.class);
ParameterizedTypeReference<String> typeReference = new ParameterizedTypeReference<String>() {};
ResponseEntity<String> responseEntity = testRestTemplate.exchange(
TARGET_API_COMP_VIBR_AND_TEMP_MONITORING_TAB,
HttpMethod.GET,
requestEntity,
typeReference
);
System.out.println(responseEntity);
}
}
проблема в том, что: если я помещаю точку взлома в метод моего класса JUnit и затем выполняю его в режиме отладки: «Отладка как -> тест JUnite» работает: он останавливается на первой найденной точке вызова в тестовый класс JUnit.
Но моя проблема в том, что он не останавливается на точке взлома, определенной в моем определении API (метод, связанный с URL-адресом в моем ExcelResource классе.
Почему? Мне кажется, что в прошлом я использовал JUnit для тестирования также поведения API и что он остановился в коде реализации API.
Что не так? Что я могу остановить на точке взлома d определено в моем коде реализации API? Чего мне не хватает?