2

I have the following problem to be solved, could you help me?

I have two methods in a class. The first generates a document (calling another class) and stores it in a string.

The second one I want to save this document number for use in other methods and in other classes, in a way that the document is the same generated initially. That is, do not generate a different document! I'm not getting ... = //

First Methods in one class (generates document, calling a method of another class):

public class oneClass {
private String cpf;
private String document() {
        if (this.cpf == null) {
            this.cpf = incluiDocumento.cpf(false);
        } else {
        }
        return this.cpf;
    }

    public void one() {
        System.out.println(document());
        System.out.println(document());
        System.out.println(document());
    }

    public void two() {
        System.out.println(document());
    }
}

Second class:

@Test
 public void testDocuments() {
     new oneClass().one();
     new oneClass().two();
 }

Conclusion: I can generate my document and store it in a string. However, in the next methods and classes, I can never use the first document ever generated. It will always generate new documents.

How can I generate a document and store it for use in tests and validate it?

Tool: Selenium Webdriver, Java.

Thanks in advance!!!

5
  • If you want to reuse the document generated in the first method, save it in a variable inside the class. Then in the second method access it with this.document. Commented Jul 20, 2020 at 13:30
  • 1
    Another option is to provide the created document as a param to the rest of logic like insertAuth(String cpf). Commented Jul 20, 2020 at 13:32
  • @marc thank you for returning. This is possible, but if I call a third method and other methods always with this.document I will have new documents. I would like to always have the same document (same variable) in my test ... Do you know? Commented Jul 20, 2020 at 14:21
  • @pirho I don't understand... Could you give me an example? Where or how i create the param? Commented Jul 20, 2020 at 14:24
  • Someone could help me?!!?! Commented Jul 20, 2020 at 20:16

1 Answer 1

2

In this case you might use this approach:

public class OneClass{    
    private String cpf;
    //...
    public String document() {
        if(this.cpf==null){
            this.cpf = document.cpf(false);
        }
        return this.cpf; 
    }
    //... method one() and two()
}

The document is created only once and saved in a class variable. Any call after that will return the saved document.

So the Second Method will always get the first document generated.

Edit:

And test it like in the following:

@Test
public void testDocuments() {
     OneClass oneClass = new OneClass();
     oneClass.one();
     oneClass.two();
}

I changed the name of your class from oneClass to OneClass because in Java class names start with capital letter.

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

6 Comments

It didn't work ... = // It runs correctly, but after my @Test, it generates a document for each method ... @Test public void testSameDocument() { new testOne; new testTwo; } The second method (testTwo) creates a new document... =// Could you help me?! Thanks in advance!
Yes, @marc ! Updated question!! Thanks in advance!
@EstevaoFPM I updated my answer, please consider to upvote ;)
@EstevaoFPM if the question is answered, please accept the answer.
Now yes!!!!!! Success!! Thank you so much for the support !! It helped a lot!!! Problem solved!!!
|

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.