I am new to RESTful web services automated testing.
I have got JSON object that has email and password. I want to change this email every time my scripts does POST while running, because if am passing the same email it fails. 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:
{
"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.
-- 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.