0

I'm using the follow code to generate random e-mails on the dataMap class:

public static String generateRandomEmail(int length) {

            String allowedChars = "abcdefghijklmnopqrstuvwxyz" + "_-.";
            String email = "";
            String temp = RandomStringUtils.random(length, allowedChars);
            email = temp.substring(0, temp.length() - 1)+"@mailinator.com";
            System.out.println(email);
            return email;    
        }

On my steps class, I need to use this email value, but I'm calling the method again, so it's generate another "email".

@Dado("que realizo a chamada no (.*) da (.*) informando (.*) e um email e (.*) novos")
        public void verificarAmbiente(String srtAmbiente, String srtAPI, String srtToken, String srtSenha) {
            System.out.println(srtAmbiente+srtAPI);
            dataMap data = new dataMap();
            int length = 15;
            data.generateRandomEmail(length);
            Map<String, String> emailContent = new HashMap<String,String>();
            emailContent.put("email", data.generateRandomEmail(length));
            Map<String, Object> postContent = new HashMap<String,Object>();
            postContent.put("customer", emailContent);
            postContent.put("password", srtSenha);
            given().contentType(ContentType.JSON)
                .header("Authorization", "Bearer "+srtToken)
                .with().body(postContent)
                .when().post(srtAmbiente+srtAPI).prettyPeek()
                .then().statusCode(200);

        }

I wanna transform the "return email" in a variable and call it in another class without execute the method again and change the value. Can you help me? Thanks!

4
  • 4
    Instead of just having data.generateRandomEmail(length); do String email = data.generateRandomEmail(length); and then use email the second time around. Commented Apr 11, 2019 at 13:28
  • 2
    If you want random email each time, you will have to execute the same block of code again and again. Commented Apr 11, 2019 at 13:28
  • The first call to generateRandomEmail is not needed at all, as it doesn't change any state. It just generates the email, returns it, but the return value is thrown away Commented Apr 11, 2019 at 13:34
  • @FedericoklezCulloca Thanks so much! It's worked :) Commented Apr 11, 2019 at 13:35

1 Answer 1

5

You need to store the generated email in a variable:

String email = data.generateRandomEmail(length);

And then you can just use the email variable later when you need the same email address.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.