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

时间:2014-07-13 12:30:49

标签: 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"
}

1 个答案:

答案 0 :(得分:0)

听起来您可以使用动态测试夹具或测试工具在每次运行时创建唯一的电子邮件地址。对于在运行之间不会在内存中持久存在的易失性测试,随机值可能很好,但时间是最佳解决方案。类似的东西:

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
相关问题