java - 如何使用 Java、Reassured、groovy 更改文件中每个 POST 请求的 Json 对象请求

标签 java json groovy

我是 RESTful Web 服务自动化测试的新手。

我有包含电子邮件和密码的 JSON 对象。我想在每次脚本运行时进行 POST 时更改此电子邮件,因为如果传递相同的电子邮件,则会失败。说电子邮件已经存在。

代码示例:

public String postPeopleUsers() throws FileNotFoundException, IOException,
            ParseException {
        Object obj = parser.parse(new FileReader(path
                + "/resources/people/postpeopleusers.json"));
        JSONObject jsonPostBody = (JSONObject) obj;
        return postRequest(jsonPostBody, usersURI, 201, "data.id",
                "postpeopleUsers(String email)", false);
    }

JSON 请求:

{
    "email": "tesrteryrt00@Testewrtt00hrtyhrerlu.com",
    "password": "test123",
    "groups": [],
    "forename": "Test",
    "surname": "Er"
}

最佳答案

听起来您可以使用动态测试装置或测试工具在每次运行时创建唯一的电子邮件地址。对于在运行之间不会保留在内存中的 volatile 测试,随机值可能很好,但时间是最好的解决方案。像这样的东西:

String email = "${System.currentTimeMillis()}@testdomain.com"

这使用当前系统时间(以毫秒为单位)(粒度约为 10 毫秒)来创建以前未使用过的新电子邮件地址。

--更新--

使用非常简单固定装置的示例测试代码:

class JSONTest extends GroovyTestCase {
    String uniqueEmail
    final String JSON_FILENAME = "/resources/people/postpeopleusers.json"

    // I don't know the name of your class under test
    SystemUnderTest sut

    @Before
    void setUp() {
        uniqueEmail = "${System.currentTimeMillis()}@testdomain.com"
        sut = new SystemUnderTest()
    }

    @After
    void tearDown() {
        // You should actually clean up the datastore by removing any new values you added during the test here

        // Also remove the persisted JSON file
        new File(JSON_FILENAME).deleteOnExit()
    }

    @Test
    void testShouldProcessUniqueEmail() {
        // Arrange
        String json = """{
    "email": "${uniqueEmail}",
    "password": "test123",
    "groups": [],
    "forename": "Test",
    "surname": "Er"
}"""

        // Write the JSON into the file you read from
        File jsonFile = new File(JSON_FILENAME)
        // Only create the parent if it doesn't exist
        !jsonFile?.parent ?: new File(jsonFile?.parent as String).mkdirs()
        jsonFile.delete()
        jsonFile.createNewFile()
        jsonFile << json

        // I don't know what you expect the response to be
        final String EXPECTED_RESPONSE = "Something good here"

        // Act
        String response = sut.postPeopleUsers()

        // Assert
        assert response == EXPECTED_RESPONSE
    }

    // This is my mock implementation which prints the read JSON to a logger and I can verify the provided email
    class SystemUnderTest {
        public String postPeopleUsers() throws FileNotFoundException, IOException,
                ParseException {
            File jsonFile = new File(JSON_FILENAME)
            String contents = jsonFile.text
            def json = new JsonSlurper().parseText(contents)

            logger.info(json)

            return "Something good here"
        }
    }
}

示例输出:

/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java -ea
15:33:49,162  INFO JSONTest:45 - {forename=Test, email=1405463629115@testdomain.com, surname=Er, password=test123, groups=[]}

Process finished with exit code 0

关于java - 如何使用 Java、Reassured、groovy 更改文件中每个 POST 请求的 Json 对象请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24722723/

相关文章:

java - 存储过程在 DB 中通过 RO 访问不可见,但通过 RW 访问可见

java - 为什么 Java 不允许定义两个除泛型类型参数外具有相同签名的方法?

java - 对多个列表中的元素进行分组

eclipse - 如何更改 Eclipse-Groovy 插件 Groovy 库?

java - 调用自定义对话框的按钮事件

java - 调用另一个类中的方法来设置标签文本(不要使用netbeans的默认内容)

javascript - 如何通过 Postman 将 JSON 发送到 NodeJS Express Server?

java - 在 JAVA 中解析具有混合已知/未知字段的 JSON

ios - 从 json 中提取问题

java - Groovy 代码 - 使用正则表达式匹配字段并在不匹配时生成空白值