用参数模拟构造函数

She*_*jie 74 java junit mocking mockito powermock

我有一个课程如下:

public class A {
    public A(String test) {
        bla bla bla
    }

    public String check() {
        bla bla bla
    }
}
Run Code Online (Sandbox Code Playgroud)

在构造函数中的逻辑A(String test)check()是我试图嘲弄的事情.我想要任何调用:new A($$$any string$$$).check()返回一个虚拟字符串"test".

我试过了:

 A a = mock(A.class); 
 when(a.check()).thenReturn("test");

 String test = a.check(); // to this point, everything works. test shows as "tests"

 whenNew(A.class).withArguments(Matchers.anyString()).thenReturn(rk);
 // also tried:
 //whenNew(A.class).withParameterTypes(String.class).withArguments(Matchers.anyString()).thenReturn(rk);

 new A("random string").check();  // this doesn't work
Run Code Online (Sandbox Code Playgroud)

但它似乎没有奏效.new A($$$any string$$$).check()仍然通过构造函数逻辑而不是获取模拟对象A.

Alb*_*ban 83

您发布的代码适用于最新版本的Mockito和Powermockito.也许你还没准备好A?试试这个:

A.java

public class A {
     private final String test;

    public A(String test) {
        this.test = test;
    }

    public String check() {
        return "checked " + this.test;
    }
}
Run Code Online (Sandbox Code Playgroud)

MockA.java

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(A.class)
public class MockA {
    @Test
    public void test_not_mocked() throws Throwable {
        assertThat(new A("random string").check(), equalTo("checked random string"));
    }
    @Test
    public void test_mocked() throws Throwable {
         A a = mock(A.class); 
         when(a.check()).thenReturn("test");
         PowerMockito.whenNew(A.class).withArguments(Mockito.anyString()).thenReturn(a);
         assertThat(new A("random string").check(), equalTo("test"));
    }
}
Run Code Online (Sandbox Code Playgroud)

两个测试都应该通过mockito 1.9.0,powermockito 1.4.12和junit 4.8.2

  • 另请注意,如果从另一个类调用构造函数,请将其包含在`PrepareForTest`的列表中 (22认同)

Ben*_*ser 45

据我所知,你不能用mockito模拟构造函数,只能模拟方法.但根据Mockito谷歌代码页上的维基,有一种方法可以通过在类中创建一个返回该类的新实例的方法来模拟构造函数行为.然后你可以模拟出那种方法.以下是直接来自Mockito wiki摘录:

模式1 - 使用单行方法创建对象

要使用模式1(测试名为MyClass的类),您可以替换类似的调用

   Foo foo = new Foo( a, b, c );
Run Code Online (Sandbox Code Playgroud)

   Foo foo = makeFoo( a, b, c );
Run Code Online (Sandbox Code Playgroud)

并写一个单行方法

   Foo makeFoo( A a, B b, C c ) { 
        return new Foo( a, b, c );
   }
Run Code Online (Sandbox Code Playgroud)

重要的是你不要在方法中包含任何逻辑; 只是创建对象的一行.原因是该方法本身永远不会进行单元测试.

当你来测试这个类时,你测试的对象实际上是一个Mockito间谍,这个方法被覆盖,返回一个模拟.因此,您正在测试的不是类本身,而是它的一个稍微修改过的版本.

您的测试类可能包含类似的成员

  @Mock private Foo mockFoo;
  private MyClass toTest = spy(new MyClass());
Run Code Online (Sandbox Code Playgroud)

最后,在你的测试方法中,用一行来模拟对makeFoo的调用

  doReturn( mockFoo )
      .when( toTest )
      .makeFoo( any( A.class ), any( B.class ), any( C.class ));
Run Code Online (Sandbox Code Playgroud)

如果要检查传递给构造函数的参数,可以使用比any()更具体的匹配器.

如果你只是想要返回你班级的模拟对象,我认为这应该适合你.无论如何,您可以在此处阅读有关模拟对象创建的更多信息:

http://code.google.com/p/mockito/wiki/MockingObjectCreation

  • +1,我不喜欢我需要调整我的源代码这一事实使它更加模拟友好.谢谢你的分享. (17认同)
  • 拥有更易测试的源代码,或者在编写代码时避免可测试性反模式,这绝非易事.如果您编写的源代码更易于测试,则它会自动更易于维护.在自己的方法中隔离构造函数调用只是实现此目的的一种方法. (17认同)
  • 编写可测试的代码很好。被迫重新设计 A 类,以便我可以为 B 类编写测试,B 类依赖于 A,因为 A 对 C 有硬编码的依赖,感觉……不太好。是的,代码最终会更好,但是我最终需要重新设计多少类才能完成一个测试的编写? (2认同)

Mev*_*mir 17

使用 Mockito,您可以使用withSettings(). 例如,如果CounterService需要 2 个依赖项,您可以将它们作为模拟传递:

 UserService userService = Mockito.mock(UserService.class);
 SearchService searchService = Mockito.mock(SearchService.class);   
 CounterService counterService = Mockito.mock(CounterService.class, withSettings().useConstructor(userService, searchService));
Run Code Online (Sandbox Code Playgroud)

  • 在我看来,这是最简单也是最好的答案。谢谢。 (3认同)

use*_*666 11

没有使用Powermock ....根据Ben Glasser的答案看下面的例子,因为我花了一些时间来弄清楚..希望节省一些时间......

原始类:

public class AClazz {

    public void updateObject(CClazz cClazzObj) {
        log.debug("Bundler set.");
        cClazzObj.setBundler(new BClazz(cClazzObj, 10));
    } 
}
Run Code Online (Sandbox Code Playgroud)

修改类:

@Slf4j
public class AClazz {

    public void updateObject(CClazz cClazzObj) {
        log.debug("Bundler set.");
        cClazzObj.setBundler(getBObject(cClazzObj, 10));
    }

    protected BClazz getBObject(CClazz cClazzObj, int i) {
        return new BClazz(cClazzObj, 10);
    }
 }
Run Code Online (Sandbox Code Playgroud)

测试类

public class AClazzTest {

    @InjectMocks
    @Spy
    private AClazz aClazzObj;

    @Mock
    private CClazz cClazzObj;

    @Mock
    private BClazz bClassObj;

    @Before
    public void setUp() throws Exception {
        Mockito.doReturn(bClassObj)
               .when(aClazzObj)
               .getBObject(Mockito.eq(cClazzObj), Mockito.anyInt());
    }

    @Test
    public void testConfigStrategy() {
        aClazzObj.updateObject(cClazzObj);

        Mockito.verify(cClazzObj, Mockito.times(1)).setBundler(bClassObj);
    }
}
Run Code Online (Sandbox Code Playgroud)


rie*_*pil 5

Mockito 3.5.0 版开始并使用InlineMockMaker,您现在可以模拟对象构造:

 try (MockedConstruction mocked = mockConstruction(A.class)) {
   A a = new A();
   when(a.check()).thenReturn("bar");
 }
Run Code Online (Sandbox Code Playgroud)

try-with-resources构造内部,所有对象构造都返回一个模拟。