Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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_Unit Testing_Powermock_Easymock - Fatal编程技术网

Java 模拟测试类中使用的不同类?

Java 模拟测试类中使用的不同类?,java,unit-testing,powermock,easymock,Java,Unit Testing,Powermock,Easymock,我怎样才能只使用powerMock或EasyMock来模拟另一个类中使用的类,我只能使用这两个框架,我知道我们可以使用Mockito,但是由于我们的代码库只包含EasyMock和powerMock库,所以我必须只使用这两个框架 我有以下代码(我正在使用powerMock) 我想模拟SecondClass.getVoidCall()方法 我的单元测试代码是 @RunWith(PowerMockRunner.class) @PrepareForTest(TestArpit.class) public

我怎样才能只使用powerMock或EasyMock来模拟另一个类中使用的类,我只能使用这两个框架,我知道我们可以使用Mockito,但是由于我们的代码库只包含EasyMock和powerMock库,所以我必须只使用这两个框架

我有以下代码(我正在使用powerMock)

我想模拟SecondClass.getVoidCall()方法

我的单元测试代码是

@RunWith(PowerMockRunner.class)
@PrepareForTest(TestArpit.class)
public class UniteTestClass {

   @Test
   public void testMock() throws Exception {
      SecondClass class2 = createMock(SecondClass.class);
      expect(class2.getVoidCall()).andReturn(20).atLeastOnce();
      expectLastCall().anyTimes();

      ClassUnderTest a=new ClassUnderTest ();
      a.getRecord();
      replayAll();
      PowerMock.verify();
}

}
2: In ArpitSecondClass getVoidCall for kv testing
基本上我希望输出如下

1: In getRecord

2: In ArpitSecondClass getVoidCall for kv testing

3:20 (Note:This should be overriden by the value I supplied in UnitTest) 

4: In getRecord over
但是我用Unitest代码得到的输出是

@RunWith(PowerMockRunner.class)
@PrepareForTest(TestArpit.class)
public class UniteTestClass {

   @Test
   public void testMock() throws Exception {
      SecondClass class2 = createMock(SecondClass.class);
      expect(class2.getVoidCall()).andReturn(20).atLeastOnce();
      expectLastCall().anyTimes();

      ClassUnderTest a=new ClassUnderTest ();
      a.getRecord();
      replayAll();
      PowerMock.verify();
}

}
2: In ArpitSecondClass getVoidCall for kv testing
代码流没有超出预期(class2.getVoidCall()).andReturn(20.atleastone()

getRecord中的其余语句不会被打印,因为它根本不会被调用

我是不是遗漏了什么

第二类#getVoidCall()方法(
publicstatic int getVoidCall(){…}
)是一种
静态
方法,因此模拟略有不同

更换前两行:

@Test
public void testMock() throws Exception {
    SecondClass class2 = createMock(SecondClass.class);
    expect(class2.getVoidCall()).andReturn(20).atLeastOnce();
使用下面的行(并准备课程):

SecondClass#getVoidCall()
方法(
publicstatic int getVoidCall(){…}
)是一种
静态方法,因此,模拟有点不同

更换前两行:

@Test
public void testMock() throws Exception {
    SecondClass class2 = createMock(SecondClass.class);
    expect(class2.getVoidCall()).andReturn(20).atLeastOnce();
使用下面的行(并准备课程):


轻松地工作。非常感谢您轻松地工作。非常感谢您使用静态方法是一种不好的做法,因为它隐藏了类的依赖关系,使您的代码不灵活,难以重用。因此,与其屈服于糟糕的设计并使用PowerMock,不如从
ArpitSecondClass
中的方法中删除
static
关键字,并将该类的实例作为构造函数参数传递给被测试的类。使用static方法是一种不好的做法,因为它隐藏了类的依赖关系,并使代码无效不灵活且难以重用。因此,与其向您屈服于糟糕的设计和使用PowerMock,不如从
ArpitSecondClass
中的方法中删除
static
关键字,并将该类的实例作为构造函数参数传递给您测试的类。