[英]Error calling method using same data (new data is always generated)
I have the following problem to be solved, could you help me?我有以下问题要解决,你能帮帮我吗?
I have two methods in a class.我在 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):一个 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:第二个 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.工具: Selenium Webdriver,Java。
Thanks in advance!!!提前致谢!!!
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.该文档仅创建一次并保存在 class 变量中。 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.我将您的 class 的名称从
oneClass
为OneClass
因为在 Java class 名称以大写字母开头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.