我遇到一个有趣的问题,当我尝试在SpringBootTest下调用它时,找不到Katharsis公开的API。我能够使用相同的方法连接到Spring MVC API,但Kartharsis端点在测试中返回HTTP 404。我可以通过外部进程连接到Kartharsis端点,例如通过浏览器调用它。
我已经尝试过使用RestTemplate或OkHTTP替换Spring TestRestTemplate的多个解决方案都有同样的问题。
以下是我的代码片段:
public class ProgramControllerTest_Search extends WebApiLayerIntegrationTest{
void "should get one program by id"() {
given:
Program program = new Program(programTitle: "Test Title")
program = this.programRepository.save(program)
when:
def response = this.testRestTemplate.getForEntity("jsonapi/programs/" + (program.id), Map.class)
then: "response status is ok"
noExceptionThrown()
response.statusCode == HttpStatus.OK
and: "response body has expected values"
response.body.data.id == program.id
response.body.data.attributes.programTitle == "Test Title"
}
}
@Slf4j
@Stepwise
@SpringBootTest(classes = [EscobarApplication.class],
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles(["integration-test", "my-local-test-config"])
public abstract class WebApiLayerIntegrationTest extends Specification {
@LocalServerPort
private int httpPort
@Autowired
protected TestRestTemplate testRestTemplate
@Autowired
private DataSourceProperties dsProp
@PostConstruct
void printTestEnvironmentInfo() {
log.info(">>> WebApiLayerIntegrationTest, using Datasource: url=${dsProp.getUrl()}, user=${dsProp.getUsername()}.")
}
protected int getHttpPort() {
return httpPort
}
}
@Entity
@Audited
@JsonApiResource(type = "programs")
public class Program extends BaseEntity {
@Id @JsonApiId
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
@Column(length = 128, name = "program_title")
@Size(min = 2, max = 128)
private String programTitle;
}