简体   繁体   English

如何使用Java,Resassured,Groovy在每个POST请求中更改文件中的Json对象请求

[英]How to change Json object request in a file every POST request using Java, Restassured, groovy

I am new to RESTful web services automated testing. 我是RESTful Web服务自动化测试的新手。

I have got JSON object that has email and password. 我有具有电子邮件和密码的JSON对象。 I want to change this email every time my scripts does POST while running, because if am passing the same email it fails. 我希望每次脚本在运行时执行POST时都更改此电子邮件,因为如果传递相同的电子邮件则会失败。 Saying the email already exist. 说电子邮件已经存在。

Code sample: 代码示例:

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 request: JSON请求:

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

Sounds like you could use a dynamic test fixture or test harness to create unique email addresses on every run. 听起来您可以在每次运行时使用动态测试装置或测试工具来创建唯一的电子邮件地址。 Random values are probably fine for volatile tests that won't persist in memory between runs, but time is the best solution. 对于在两次运行之间不会保留在内存中的易失性测试,随机值可能很好,但是时间是最好的解决方案。 Something like: 就像是:

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

This uses the current system time in milliseconds (with granularity around 10 ms) to create a new email address that won't have been used previously. 这将使用当前系统时间(以毫秒为单位)(粒度大约为10 ms)来创建以前不会使用的新电子邮件地址。

-- Update -- -更新-

Example test code which uses very simple fixture: 使用非常简单的夹具的示例测试代码:

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"
        }
    }
}

Example output: 输出示例:

/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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM