Java 使用相同数据调用方法时出错(始终生成新数据)
我有以下问题需要解决,你能帮我吗 我在一个类中有两个方法。 第一个类生成一个文档(调用另一个类)并将其存储在字符串中 第二个,我想保存这个文档编号,以便在其他方法和其他类中使用,使文档与最初生成的文档相同。也就是说,不要生成不同的文档! 我不会// 一个类中的第一个方法(生成文档,调用另一个类的方法):Java 使用相同数据调用方法时出错(始终生成新数据),java,selenium-webdriver,intellij-idea,automation,Java,Selenium Webdriver,Intellij Idea,Automation,我有以下问题需要解决,你能帮我吗 我在一个类中有两个方法。 第一个类生成一个文档(调用另一个类)并将其存储在字符串中 第二个,我想保存这个文档编号,以便在其他方法和其他类中使用,使文档与最初生成的文档相同。也就是说,不要生成不同的文档! 我不会// 一个类中的第一个方法(生成文档,调用另一个类的方法): public class oneClass { private String cpf; private String document() { if (this.cpf == n
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());
}
}
@Test
public void testDocuments() {
new oneClass().one();
new oneClass().two();
}
二等舱:
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());
}
}
@Test
public void testDocuments() {
new oneClass().one();
new oneClass().two();
}
结论:
我可以生成文档并将其存储在字符串中。但是,在接下来的方法和类中,我永远不能使用生成的第一个文档。它将始终生成新文档
如何生成文档并将其存储以用于测试和验证?
工具:Selenium Webdriver,Java
提前感谢 在这种情况下,您可以使用以下方法:
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()
}
文档只创建一次并保存在类变量中。之后的任何调用都将返回保存的文档
因此,第二个方法将始终生成第一个文档
编辑:
并按如下方式进行测试:
@测试
公共文件(){
OneClass OneClass=新的OneClass();
一类。一();
一类。二();
}
我将类的名称从
oneClass
更改为oneClass
,因为在Java中,类名以大写字母开头。如果要重用在第一个方法中生成的文档,请将其保存在类内的变量中。然后在第二种方法中,使用this.document
访问它。另一个选项是将创建的文档作为参数提供给其他逻辑,如insertAuth(String cpf)
@marc谢谢您返回。这是可能的,但是如果我调用第三个方法和其他方法时总是使用This.document
,我将拥有新文档。我希望在我的测试中总是有相同的文档(相同的变量)。。。你知道吗?@pirho我不明白。。。你能给我举个例子吗?我在哪里或如何创建参数?有人可以帮助我?!!?!它不起作用…=/它运行正常,但在我的@Test之后,它会为每个方法生成一个文档@Test public void testSameDocument(){new testOne;new testTwo;}
第二个方法(testTwo)创建一个新文档…=/你能帮我吗?!提前谢谢!是的,马克!更新问题!!提前谢谢@我更新了我的答案,请考虑投票表决;如果问题得到了回答,请接受答案。现在是!!!!!!成功!!非常感谢您的支持!!这帮了大忙!!!问题解决了!!!