简体   繁体   English

如何从测试类中访问私有方法

[英]how can I access a private method from the test class

I have some methods which are private in the class example and I want to use them in the test class for testing purposes, how can I access these methods and leave them as private 我有一些在类示例中是私有的方法,我想在测试类中使用它们进行测试,如何访问这些方法并将它们保存为私有

import java.util.*;

public class Example
{
      Scanner scanner;
    public Example()
    {
       scanner = new Scanner(System.in);
    }
    private void enterName()
    {
        System.out.println("Enter name");
        String name = scanner.nextLine();
        System.out.println("Your name is: " + name);
    }
    private void enterAge()
    {
         System.out.println("Enter age");
        int age = scanner.nextInt();
        System.out.println("Your age is : " + age); 
    }
    public void userInput()
    {
        enterAge();
        enterName();
    }
}


public class Test
{

  public static void main(String args[])
  {
      Example n = new Example();
      n.enterName();
      n.enterAge();
    }

}

Why would you test the private methods while one will only use the public one? 为什么要测试私有方法,而只使用公共方法? Unit testing is about testing for expected behavior. 单元测试是关于预期行为的测试。 Public methods expose that behavior. 公共方法暴露了这种行为。

If you want to test the output generated you could implement a protected method to write to out eg 如果你想测试产生的输出,你可以实现一个受保护的方法写out

public class Example {

    // code omitted 

    private void enterName() {
        writeMessage("Enter name");
        String name = scanner.nextLine();
        writeMessage("Your name is: " + name);
    }

    protected void writeMessage(String msg) {
        System.out.println(msg);
    }
}

In your test you could then create a private class which extends Example and overrides the writeMessage method. 在测试中,您可以创建一个扩展Example的私有类,并覆盖writeMessage方法。

public class ExampleTest {

    public testOutput() {
        MyExample e = new MyExample();
        e.userInput();

        String output = e.getOutput();

        // test output string
    }

    private class MyExample extends Example {
        private String output = "";

        public String getOutput() {
            return output;
        }

        @Override
        public void writeMessage(String msg) {
            output += msg;
        }
    }
}

You also might want a setter or constructor which can inject the Scanner object. 您可能还需要一个可以注入Scanner对象的setter或构造函数。 This will make testing easier since you could then inject a mocked scanner version. 这将使测试更容易,因为您可以注入模拟的扫描仪版本。

Unit-testing is indeed black-box testing, so you cannot, and should not, access the private (inner mechanisms) methods. 单元测试确实是黑盒测试,因此您不能也不应该访问私有(内部机制)方法。 However, white-box testing is often called assertion-based verification. 但是,白盒测试通常称为基于断言的验证。 Just put assertions (or it-then-throw statements) everywhere, and your black-box unit test will become a white-box test. 只需将断言(或it-then-throw语句)放在任何地方,您的黑盒单元测试将成为白盒测试。

More specifically, try to put pre-conditions and post-conditions as much as possible in your private methods to verify the inputs and the outputs. 更具体地说,尝试在您的私有方法中尽可能多地放置前置条件和后置条件,以验证输入和输出。 Therefore, you define a contract between every method caller and callee. 因此,您在每个方法调用者和被调用者之间定义一个契约。 If something goes wrong, you'll quickly see where and why. 如果出现问题,您将很快看到地点和原因。

See Design-by-Contract for more info! 有关详细信息,请参阅合同设计

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM