我遇到了一个有趣的问题,当我尝试在 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;
}