简体   繁体   English

使用相同数据调用方法时出错(总是生成新数据)

[英]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 的名称从oneClassOneClass因为在 Java class 名称以大写字母开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用同一类的数据类型的受保护方法时出错 - Error using protected method of data type of the same class 对同一方法使用不同的数据类型 - Using different data types for the same method Java“无法取消引用double”错误-double数据类型上的调用方法 - Java “double cannot be dereferenced” error - calling method on double data type 调用rest api时,无论是否使用lomboks @Data,方法均未定义 - Method is undefined regardless using lomboks @Data when calling rest api 使用@Async调用方法与在新线程中调用方法 - Calling a method using @Async vs Calling the method in a new Thread Spring Data MongoDB 始终将对象标识为新对象 - Spring Data MongoDB always identifies object as new 在我的数据库 Java Derby 中插入新数据时生成的 ID 错误 - Error with the ID generated when i insert new data in my database Java Derby Weka总是为不同的数据生成相同的集群 - Weka always producing same clusters for different data Java从主函数调用print方法并使用来自另一个单独方法的数据 - Java calling a print method from a main function and using data from another separate method hashcode是在调用hashcode()方法时生成的 - hashcode is generated on calling hashcode() method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM