扩展抽象类的测试类 [英] Testing class that extends abstract class

查看:81
本文介绍了扩展抽象类的测试类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为扩展抽象类的类编写测试.当我需要测试具有super引用的方法时,问题就开始了.所以代码看起来像这样:

I need to write a test for a class that extends an abstract class. Problem starts when I need to test a method that has super reference. So the code looks something like this:

public abstract class AbstractClass{
    public String someMethod();
}

public class MyClass extends AbstractClass {
    methodToTest(){
        //somecode
        super.someMethod();
    }
}

任何想法我应该如何解决?

Any idea how should I get around it?

对不起,我还是单元测试的新手,不久前我就弄清楚了为什么这完全不符合我的意愿.上面的示例中的MyClass实际上包含一个初始化块,该块设置一些在AbstractClass中声明的字段,这些字段由someMethod使用,因此new AbstractClass.someMethod()给出的结果与super.someMethod相同.而且,由于我无法初始化AbstractClass,因此不可能获得期望的结果.我认为我需要以某种方式对其进行嘲笑,但是正如我所说的,我对此并不陌生,不知道该怎么做

I'm sorry, I am still new to unit testing and just a moment ago figured out why exactly this is not working as I would like to. MyClass in the example above actually contains a initialization block that sets some fields declared in AbstractClass that are used by someMethod so new AbstractClass.someMethod() didn't give me the same results as super.someMethod. And, as I cannot initialize the AbstractClass it would be impossible to get desired results. I figured that I would need to mock it somehow but as I said I am new to this and have no idea how to do that

public abstract class AbstractClass{
    String fieldA;
    public String someMethod(){
        //does something with fieldA
    }
} 

public class MyClass extends AbstractClass {
    {
        setFieldA("something");
    }
    methodToTest(){
        //some code
        super.someMethod();
        //returns some string based on the fieldA value
   }
}

更新,这是我的代码的更新版本.请帮我写测试.我也许可以从那里得到它:

UPDATE Here is updated version of my code. Please help me with writing test for that. I would probably be able to get it from there:

public abstract class AbstractClass{
    String fieldA;
    public String someMethod(){
        fieldA = "String" + fieldA;
    }
} 

public class MyClass extends AbstractClass {
    {
        setFieldA("Another String");
    }
    methodToTest(){
        fieldA = fieldA + "Yet Another String";
        super.someMethod();
        return fieldA;
   }
}

推荐答案

您的问题与单元测试无关;但更多的是关于如何使用抽象类的普遍误解.

Your problem is less about unit testing; but more a general misconception how to work with abstract classes.

首先:如果您的抽象类需要一个字段来完成其工作,那么您最好为此使用setter.相反:

First of all: if your abstract class needs a field to do its job, then you better not use a setter for that. Instead:

public abstract class Base {
  private final String thatField;
  public Base(String incoming) {
    thatField = incoming;
  }
..

public class Derived extends Base {
   public Derived(String incoming) {
     super(incoming);
   }

换句话说:该字段似乎是类的重要部分.因此,使之明确.通过启用通过构造函数进行设置,您还可以确保

In other words: it seems that this field is an important part of your classes. So make that explicit. And by enabling it to be set via constructor, you also ensure that

  • 您可以控制何时/如何设置字段
  • 您会从编译器获得帮助-通过将该字段设置为final,可以在忘记初始化时得到错误

除此之外:当您开始测试某个类时...该类是否仅扩展Object无关紧要;或者如果它扩展了其他类别;或者即使扩展抽象类也是如此.

Beyond that: when you start testing some class ... it shouldn't matter if that class just extends Object; or if it extends some other class; even when extending an abstract class.

关于这里可以说的是什么;鉴于您的示例没有包含有关代码真正"在做什么的任何特定细节.

That is about what can be said here; given the fact that your example doesn't contain any specific details about what your code is "really" doing.

这篇关于扩展抽象类的测试类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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