Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单身人士的测试日期_Java_Date_Testing_Singleton - Fatal编程技术网

Java 单身人士的测试日期

Java 单身人士的测试日期,java,date,testing,singleton,Java,Date,Testing,Singleton,我总是以这样的方式测试日期,在我的类中有一个受保护的方法getDate(),我为了测试的目的重写了它。例如: public class MyClass { protected Date getDate(){ return new Date(); }; } 测试: 到今天为止,这已经足够了,但出现了以下问题: 我有一个类要实例化一次,所以我使用单例模式: public static Something getInstance() { if (instanc

我总是以这样的方式测试日期,在我的类中有一个受保护的方法getDate(),我为了测试的目的重写了它。例如:

public class MyClass {
    protected Date getDate(){
        return new Date();
    };
}
测试:

到今天为止,这已经足够了,但出现了以下问题: 我有一个类要实例化一次,所以我使用单例模式:

public static Something getInstance() {
    if (instance == null) {
        instance = new Something();
    }

    return instance;
}

问题是我必须这样创建一个对象:在该方法中创建了Something.getInstance()-对象。我不能在getInstance中重写getDate方法,因为它没有意义(有时我需要实际实例,有时需要测试实例)。这就引出了一个问题:在这种情况下如何测试日期?

听起来您需要使用捕获功能(假设EasyMock是一个选项)

如果将getDate()方法放在singleton上,那么它就有了所谓的方法,这几乎不可能为测试目的进行模拟。您唯一的选择是使用测试框架,或者将类更改为不是“真正的”单例,同时以其他方式确保只能实例化它的一个实例。我认为后一种方法是正确的,在这种情况下不应该使用单例。

正如您刚才注意到的,每次使用普通的旧java单例时,都会创建不稳定的代码

还有其他方法可以使一个类只有一个实例。从简单-只需创建一次:)到使用像Spring这样的IoC框架让他们关心它


我建议您看一看,关于全局状态的部分。

如果并且只有当单例模式无论如何都无法避免时,您才可以使用以下方法模拟
Something
类的构造:

您必须模拟Something类的构造函数,以始终返回您创建的实例(注意,但这样做也会覆盖
Something
类的所有其他构造,但在这种情况下,由于这是一个单例,因此似乎可以):

此外,还可以在
Something
类上模拟getInstance函数:

//mock the static functions
mockStatic(Something.class);          

expect(Something.getInstance()).andReturn(mockSomethingInstance);
//have to replay the class in order to work
replay(IdGenerator.class); 
但是,我假设您想这样做,因为您想测试类本身的行为。但这开始变得越来越丑陋


总而言之:在使用单例之前要三思而后行-它们看起来很简单(当然,只是看起来…),从长远来看,它们很快就会变成噩梦。。。使用Spring是一种更干净的方法,可以实现同样的功能,省去很多麻烦。

使用任何最新的模拟框架,如Powermock。这就是今天要做的…你到底想做什么?测试日期的目标是什么?如果您只是检查是否正确创建了日期,那么您不必测试它们,它们已经被测试过了。不要使用所谓的
testall
方法。相反,如果日期对您的算法/方法/任何东西都有影响,那么您应该考虑使用模拟框架(如JMock、EasyMock、Mockito、Powermock等)+1作为链接:始终是一个有趣的读物;-)还有在为时已晚之前杀了单身汉的建议。
//create mock instance,     
Something mockSomethingInstance = createMock(Something.class);
//create your Date instance to return
Date d = new Date();
when(mockSomethingInstance.getDate()).thenReturn(d);
expectNew(Something.class).andReturn(mockSomethingInstance); 
//mock the static functions
mockStatic(Something.class);          

expect(Something.getInstance()).andReturn(mockSomethingInstance);
//have to replay the class in order to work
replay(IdGenerator.class);