在java中的字符串中追加值(Appending values in a string in java)

我有一个REST API测试套件,其中重复使用某些URI。 因此,我创建了一个具有public static final成员的单独类。 就像是:

public class RestURI { public RestURI(){} public static final String getAllShipsURI = "/ship/manager/ships"; public static final String getAllPortsURI = "/port/manager/ports"; }

但是,有没有办法处理这样的URI:

/infrastructure/ships/docked/" + shipId + "/capacity

我正在寻找一种方法,以便我可以在RestURI类中声明上面的URL,并在我使用它们时仍然在测试中指定值。

I have a REST API test suite where certain URIs are used repeatedly. Thus, I created a separate class with public static final members. Something like:

public class RestURI { public RestURI(){} public static final String getAllShipsURI = "/ship/manager/ships"; public static final String getAllPortsURI = "/port/manager/ports"; }

However, is there a way to deal with URIs like this:

/infrastructure/ships/docked/" + shipId + "/capacity

I am looking for a way such that I can declare the URL like above in the RestURI class and still specify values in the test when I use them.

最满意答案

您可以使用常量格式而不是String并使用静态getter:

public static String getShipUri(int shipId) { return String.format("/infrastructure/ships/docked/%d/capacity", shipId); }

You can use a constant format, rather than a String and use a static getter:

public static String getShipUri(int shipId) { return String.format("/infrastructure/ships/docked/%d/capacity", shipId); }

更多推荐