简体   繁体   English

如何为调用其他方法的方法编写单元测试?

[英]How to write unit tests for method which calls other methods?

This is a really bad sample of code. 这是一个非常糟糕的代码示例。 Sorry about that. 对于那个很抱歉。 I want to write a unit test for myMethod(). 我想为myMethod()写一个单元测试。 This only calls other methods and return String str1. 这仅调用其他方法并返回String str1。 I don't see there is any need of testing this code. 我看不到需要测试此代码。

public class MyClass{
    private String str1;
    private String str2;

    private void m1(){..}
    private String m2(){  
         // do something
        return someString;

    }
    public String myMethod(String s1){
      str2 = s1;
      m1();
      str1 = m2();
      return str1;
    }
}

There are various ways to write unit tests. 有多种编写单元测试的方法。

A) You observe behavior. A)您观察行为。 You create an object of your class under test, then you call methods; 您创建要测试的类的对象,然后调用方法。 and then you assertThat on the values that are returned. 然后你assertThat上返回的值。

B) When your object uses other objects, then you might have to turn to mocking frameworks, for example to verify that you see the expected method calls on those objects. B)当您的对象使用其他对象时,则可能必须转向模拟框架,例如,以验证您是否看到了对这些对象的预期方法调用。

In your case, it really depends on what m1/m2 are doing. 在您的情况下,这实际上取决于m1 / m2在做什么。 As said - the best unit tests are those that only check the "observable" behavior of your code under test. 如前所述-最好的单元测试是那些检查被测代码的“可观察”行为的单元测试。 Like those examples: 像那些例子:

@Test(expected=NullPointerException.class)
public void testMyMethodWithNull() {
  new MyClass().myMethod(null);
}

The above would check that when calling your method with null ... a NPE is thrown. 上面将检查以null调用您的方法时...抛出NPE。

@Test
public void testMyMethodWithEmptyString() {
  MyClass underTest = new MyClass();
  assertThat(underTest.myMethod(""), is("some expected string for empty input"));
}

That one does some different checking for EMPTY input. 那个为空输入做了一些不同的检查。

And so you work your way through all inputs that make sense. 这样,您便可以通过所有有意义的输入来工作。 Of course, the idea here is that all possible behavior of your class under test can be checked this way. 当然,这里的想法是可以用这种方法检查被测类的所有可能的行为。 If there are others that come into play, you have to take them into consideration of course. 如果还有其他因素在起作用,那么您当然必须考虑它们。 But ideally, that should not be the case: you should design all your code so that it can be fully tested as easily as possible; 但是理想情况下并非如此:您应该设计所有代码,以便尽可能容易地对其进行全面测试; and ideally without the need to write anything else but such kind of tests. 理想情况下,除了此类测试外,无需编写其他任何内容。

I completely agree with GhostCat : 我完全同意GhostCat

[…] the best unit tests are those that only check the "observable" behavior of your code under test. […]最好的单元测试是那些仅检查被测代码的“可观察”行为的单元测试。

Therefore, only test the public contract of your class under test (CUT). 因此,仅测试您被测课程(CUT)的公共合同。

However, there're rare situations in which you may need to test private methods, eg if you're working with legacy code. 但是,在极少数情况下,您可能需要测试私有方法,例如,如果您正在使用旧版代码。 One way to do this is with PowerMockito : 一种方法是使用PowerMockito

PowerMockito.verifyPrivate(cut).invoke("m1");

Where cut is an instance of your CUT – MyClass . cut是您的CUT – MyClass的实例。

Checkout these questions for further opinions and references: 请查看以下问题,以获取进一步的意见和参考:

"I don't see there is any need of testing this [private method]." “我认为没有必要测试这种[私有方法]。”

Yes. 是。 That's right. 那就对了。

A method that is private doesn't need any direct testing. 私有方法不需要任何直接测试。 Of course, private methods should be covered , but since they are private and NOT a part of your exposed API, they don't need to be tested as directly as public methods. 当然,私有方法应该包括在内 ,但是由于它们是私有的,并且不是公开API的一部分,因此不需要像public方法一样直接对其进行测试。

This book-- JUnit in Action: Second Edition --outlines many strategies for using JUnit effectively. 本书《 JUnit在行动:第二版》概述了有效使用JUnit的许多策略。 Implementing TDD is one way to make sure that your class's non-public functionality is well-covered by your tests. 实施TDD是确保测试很好地覆盖类的非公共功能的一种方法。 That is, you write your unit tests first , then you implement your class to pass those tests. 也就是说,你写你的单元测试然后再你实现你的类来通过这些测试。 In this way, you only write functionality that you need, as defined by your tests. 这样,您只需编写测试定义的所需功能。

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

相关问题 我们如何为涉及与数据库连接的方法编写单元测试? - How do we write unit tests for methods which involves connection with DB? 使用Mockito调用多个其他方法的方法的单元测试 - Unit test for method that calls multiple other methods using Mockito 如何在 Spring 中为事务方法编写事务单元测试? - How to write Transactional Unit Tests for Transactional Methods in Spring? 如何为使用硬件资源的类编写单元测试? - How to write unit tests for classes which use hardware resources? 如何为私有服务方法和内部私有方法编写单元测试? - How can I write unit tests for private methods of service and for public methods with private inside? 如何命名只调用其他方法的类/方法? - How do you name a class/method that only calls other methods? 如何模拟一个方法来调用同一类中存在的其他方法 - How to mock a method that calls other methods that are present in the same class 如何为这种类型的方法编写单元测试用例或模拟,或者我的自定义方法 removeUsers 对于单元测试来说不够好? - How to write unit test cases or mock for this type of method or is my custom method removeUsers not good enough for unit tests? 如何编写此类的单元测试 - How to Write unit tests for this class 如何编写Jemmy单元测试? - How to write Jemmy unit tests?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM