我有一个要运行的集成测试类。该类在下面提供,
@EnableItKafkaSupport
public class RiskEngineControllerIT extends ItWebTests {
public static final String URL_TEMPLATE = "http://localhost:%s/iris-web%s";
private RequestInformation requestInfo;
private final URI uri;
public RiskEngineControllerIT() {
uri = URI.create( format( URL_TEMPLATE, tomcatPort, API_V2 ) );
}
@Override
@Before
public void setUp() {
super.setUp();
requestInfo = new TimestampRequestInformation();
ratepayHeader( requestInfo );
}
@Test
public void successfulFraud() {
TestDataSet testData = TestDataLibrary.fraud();
setupMockForVera( testData );
ResponseAsserter response = executeRequest( testData );
assertIrisResult( response, testData.getOriginalData().get( PRODUCT ), IRIS_REJECT );
assertKafka( testData.getOriginalData() );
}
// .... more code
}
我对代码库不太熟悉。但是,当我运行测试时,我得到下面提供的错误堆栈,
java.lang.AssertionError: target/it.properties (No such file or directory)
at org.junit.Assert.fail(Assert.java:88)
at com.ratepay.commons.it.ItTests.loadPorts(ItTests.java:113)
at com.ratepay.commons.it.ItTests.<init>(ItTests.java:57)
at com.ratepay.commons.it.web.ItWebTests.<init>(ItWebTests.java:43)
at com.ratepay.iris.web.controller.RiskEngineControllerIT.<init>(RiskEngineControllerIT.java:70)
出于好奇,我根据要求提供了文件target/it.properties
。错误消失了,但是出现了新的错误。
java.lang.IllegalArgumentException: 'tomcatPort' must not be NULL or empty
at org.springframework.util.Assert.hasText(Assert.java:284)
at com.ratepay.commons.it.ItTests.loadPorts(ItTests.java:110)
at com.ratepay.commons.it.ItTests.<init>(ItTests.java:57)
at com.ratepay.commons.it.web.ItWebTests.<init>(ItWebTests.java:43)
at com.ratepay.iris.web.controller.RiskEngineControllerIT.<init>(RiskEngineControllerIT.java:70)
如果我向文件提供更多信息,则可以继续进行。据我了解,该文件应从ItTests
类自动生成。
tomcat.port=9090
kafka.port=9999
集成测试扩展了此类
public abstract class ItWebTests extends ItTests {
protected RestTemplate restTemplate;
private ItMockSupport itMockSupport;
private final Map<String, String> defaultHeaders;
public ItWebTests() {
this.initRestTemplate();
this.defaultHeaders = new HashMap();
}
@After
public void tearDown() {
super.tearDown();
Optional.ofNullable(this.itMockSupport).ifPresent(ItMockSupport::tearDown);
}
private void initRestTemplate() {
this.restTemplate = new RestTemplate();
this.restTemplate.setErrorHandler(new SilentRestErrorHandler());
}
protected ItMockSupport itmock() {
if (Objects.isNull(this.itMockSupport)) {
this.itMockSupport = new ItMockSupport(this.restTemplate, this.tomcatPort);
}
return this.itMockSupport;
}
protected ItWebTests defaultHeader(String name, String value) {
this.defaultHeaders.put(name, value);
return this;
}
protected ItWebTests ratepayHeader(RequestInformation requestInfo) {
this.defaultHeaders.put("RP-Gateway-Request-Id", requestInfo.getGatewayRequestId());
this.defaultHeaders.put("RP-Transaction-Id", requestInfo.getTransactionId());
return this;
}
protected ItWebTests ratepayHeader() {
return this.ratepayHeader(new TestRequestInformation());
}
protected ResponseAsserter postJson(Object dto, URI uri) {
return this.jsonRequest(dto, RequestEntity.post(uri));
}
protected ResponseAsserter putJson(Object dto, URI uri) {
return this.jsonRequest(dto, RequestEntity.put(uri));
}
protected ResponseAsserter getJson(URI uri) {
return this.jsonRequest(RequestEntity.get(uri));
}
protected ResponseAsserter deleteJson(URI uri) {
return this.jsonRequest(RequestEntity.delete(uri));
}
protected ResponseAsserter jsonRequest(Object dto, BodyBuilder builder) {
builder.contentType(MediaType.APPLICATION_JSON_UTF8);
this.jsonHeader(builder);
return this.submit(builder.body(dto));
}
protected ResponseAsserter jsonRequest(HeadersBuilder<?> builder) {
this.jsonHeader(builder);
return this.submit(builder.build());
}
private void jsonHeader(HeadersBuilder<?> builder) {
this.defaultHeaders.forEach((x$0, xva$1) -> {
builder.header(x$0, new String[]{xva$1});
});
}
private ResponseAsserter submit(RequestEntity<?> entity) {
return new ResponseAsserter(this.restTemplate.exchange(entity, String.class));
}
}
此类扩展了ItTests
abstract
类。
public abstract class ItTests {
protected ObjectMapper mapper = new ObjectMapper();
private ItDatabaseSupport databaseSupport;
private ItKafkaSupport kafkaSupport;
private ItRedisSupport redisSupport;
private ItMongoSupport mongoSupport;
protected String tomcatPort;
protected String kafkaPort;
protected String zookeeperPort;
protected String mongoPort;
protected String hsqlPort;
protected String postgresPort;
protected String redisPort;
protected String elasticSearchPort;
public ItTests() {
this.loadPorts();
}
@Before
public void setUp() {
if (this.isKafkaSupportEnabled()) {
this.kafka().setUp(this.kafkaPort);
}
if (this.isRedisSupportEnabled()) {
this.redis().setUp(this.redisPort, ((EnableItRedisSupport)this.getClass().getAnnotation(EnableItRedisSupport.class)).database());
}
if (this.isMongoSupportEnabled()) {
this.mongo().setUp(this.mongoPort);
}
}
@After
public void tearDown() {
if (this.isKafkaSupportEnabled()) {
this.kafka().tearDown();
}
if (this.isRedisSupportEnabled()) {
this.redis().tearDown();
}
if (this.isMongoSupportEnabled()) {
this.mongo().tearDown();
}
}
private void loadPorts() {
try {
InputStream is = new FileInputStream(new File("target/it.properties"));
Throwable var2 = null;
try {
Properties prop = new Properties();
prop.load(is);
this.tomcatPort(prop.getProperty("tomcat.port"));
this.postgresPort(prop.getProperty("postgres.port"));
this.mongoPort(prop.getProperty("mongo.port"));
this.hsqlPort(prop.getProperty("hsql.port"));
this.kafkaPort(prop.getProperty("kafka.port"));
this.zookeeperPort(prop.getProperty("zookeeper.port"));
this.redisPort(prop.getProperty("redis.port"));
this.elasticSearchPort(prop.getProperty("elasticsearch.port"));
Assert.hasText(this.tomcatPort, "'tomcatPort' must not be NULL or empty");
} catch (Throwable var12) {
var2 = var12;
throw var12;
} finally {
if (is != null) {
if (var2 != null) {
try {
is.close();
} catch (Throwable var11) {
var2.addSuppressed(var11);
}
} else {
is.close();
}
}
}
} catch (IOException var14) {
org.junit.Assert.fail(var14.getMessage());
}
}
protected synchronized ItDatabaseSupport database() {
if (Objects.isNull(this.databaseSupport)) {
this.databaseSupport = new ItDatabaseSupport();
}
return this.databaseSupport;
}
protected ItKafkaSupport kafka() {
if (Objects.isNull(this.kafkaSupport)) {
this.kafkaSupport = new ItKafkaSupport();
}
return this.kafkaSupport;
}
protected ItRedisSupport redis() {
if (Objects.isNull(this.redisSupport)) {
this.redisSupport = new ItRedisSupport();
}
return this.redisSupport;
}
protected ItRedisSupport redis(ObjectMapper mapper) {
if (Objects.isNull(this.redisSupport)) {
this.redisSupport = new ItRedisSupport(mapper);
}
return this.redisSupport;
}
protected ItMongoSupport mongo() {
if (Objects.isNull(this.mongoSupport)) {
this.mongoSupport = new ItMongoSupport();
}
return this.mongoSupport;
}
protected void tomcatPort(String tomcatPort) {
this.tomcatPort = tomcatPort;
}
protected void kafkaPort(String kafkaPort) {
this.kafkaPort = kafkaPort;
}
protected void zookeeperPort(String zookeeperPort) {
this.zookeeperPort = zookeeperPort;
}
protected void mongoPort(String mongoPort) {
this.mongoPort = mongoPort;
}
protected void hsqlPort(String hsqlPort) {
this.hsqlPort = hsqlPort;
}
protected void postgresPort(String postgresPort) {
this.postgresPort = postgresPort;
}
protected void redisPort(String redisPort) {
this.redisPort = redisPort;
}
protected void elasticSearchPort(String redisPort) {
this.elasticSearchPort = redisPort;
}
private boolean isKafkaSupportEnabled() {
return Objects.nonNull(this.getClass().getAnnotation(EnableItKafkaSupport.class));
}
private boolean isRedisSupportEnabled() {
return Objects.nonNull(this.getClass().getAnnotation(EnableItRedisSupport.class));
}
private boolean isMongoSupportEnabled() {
return Objects.nonNull(this.getClass().getAnnotation(EnableItMongoSupport.class));
}
}
后面的2个类来自库,并作为项目的maven依赖项添加。因此,在内心深处我可以看到提到的"target/it.properties"
,但是,我不确定该如何做。
private void loadPorts() {
try {
InputStream is = new FileInputStream(new File("target/it.properties"));
Throwable var2 = null;
// .... some code
}
如何解决该错误,我是否需要提供任何配置以在IDE中运行集成测试?我现在使用IntelliJ
任何建议都会有所帮助。谢谢。