为一个方法创建一个Junit测试用例,其中包含对不同类的另一个方法的调用,该调用又调用了同一类的不同方法 [英] Create a Junit test case for a method which contains a call to another method from different class which calls to a different method of the same class

查看:688
本文介绍了为一个方法创建一个Junit测试用例,其中包含对不同类的另一个方法的调用,该调用又调用了同一类的不同方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java和单元测试的新手.我在向一种测试方法编写单元测试时遇到问题.您能帮我吗?

I am new to java and Unit testing. I am facing a problem in writing a unit test to one of the test methods. Can you please help me with this.

ClassA:

Class   classA{ 
    /*......*/
    classB  method1_ClassA(parameters){
        /*.....
        .....
        .....
        .....
        .....*/
        String  some_Identifier =   method2_ClassA(parameters)

    }
    private String  method2_ClassA(parameters){
        /*contains call to the database*/
    }
}

ClassB:

Class classB{
    /*.....*/
}

ClassC:

Class   classC{
    /*.......*/
    public  classB  method1_ClassC(parameters){
            classA  obj_ClassA  =   new classA();
            return  obj_ClassA.method1_classA(parameters);
    }
    /*.......*/

}

我正在尝试对classC进行单元测试. ClassC中的一种方法调用类A的方法.我尝试模拟此方法,即method1_classA.但是此方法调用了同一类中存在的另一个方法.有人可以帮我如何为classC创建单元测试.

I am trying to do Unit Testing to the classC. One of the methods in ClassC calls a method of class A. I tried to mock this method, which is method1_classA. But this method has a call to another method which is present in the same class. Can somebody please help me on how to create a unit test for classC.

推荐答案

我认为我现在正在解决您的问题.问题是您创建了不可测试的代码.就这么简单:

I think I am getting your problem by now. The problem is that you created untestable code. It is as simple as that:

class C {
  public B foo(parameters){
    A someA new A();
    return  someA.someMethod(parameters);

(请注意:我对您的示例进行了重新设计,以使用对Java程序员来说不太混乱的名称.例如,请勿在类名中添加"class")

(please note: I reworked your example to use names that aren't so confusing for java programmers. You do not put "class" into the name of a class for example)

您的代码存在问题,是调用.简而言之,该调用几乎不可能对该方法进行合理的单元测试.这里唯一合理的答案是:重做您的生产代码,并引入依赖项注入,例如

The problem with your code is that call to new. Simply spoken, that call makes it almost impossible to do reasonable unit testing for that method. The only reasonable answer here is: rework your production code, and introduce dependency injection, like

class C {
  private A someA;

  public C() { this(new A()); }
  C(A someA) { this.someA = someA; }

  public B foo(parameters){            
    return  someA.someMethod(parameters);

在那里看到的东西:有一个内部"构造函数,可用于提供A对象.您可以使用此构造函数将模拟的A赋予C.突然之间,您可以测试整个过程.

What you see there: there is an "internal" constructor that you can use to provided an A object. You use this constructor to give a mocked A to C; and all of a sudden, you can test the whole thing.

长话短说:

a)我真的建议更改此代码;因为它的设计不当,并且到目前为止根本无法测试

a) I really advise to change this code; because it is badly designed, and simply not testable as of now

b)如果您确实无法做到;您可能想看看Powermock是否可以在这里为您提供帮助...但是请注意:这不是初学者的工具.在许多层面上它都是强大而危险的.

b) If you really can't do that; you might want to look if Powermock can help you here ... but beware: that is not a tool for beginners. It is powerful and dangerous on many levels.

无论如何,请观看 ...

In any case, watch this ...

这篇关于为一个方法创建一个Junit测试用例,其中包含对不同类的另一个方法的调用,该调用又调用了同一类的不同方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆