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
Ben*_*ser 45
据我所知,你不能用mockito模拟构造函数,只能模拟方法.但根据Mockito谷歌代码页上的维基,有一种方法可以通过在类中创建一个返回该类的新实例的方法来模拟构造函数行为.然后你可以模拟出那种方法.以下是直接来自Mockito wiki的摘录:
模式1 - 使用单行方法创建对象
要使用模式1(测试名为MyClass的类),您可以替换类似的调用
Run Code Online (Sandbox Code Playgroud)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 ); }重要的是你不要在方法中包含任何逻辑; 只是创建对象的一行.原因是该方法本身永远不会进行单元测试.
当你来测试这个类时,你测试的对象实际上是一个Mockito间谍,这个方法被覆盖,返回一个模拟.因此,您正在测试的不是类本身,而是它的一个稍微修改过的版本.
您的测试类可能包含类似的成员
Run Code Online (Sandbox Code Playgroud)@Mock private Foo mockFoo; private MyClass toTest = spy(new MyClass());最后,在你的测试方法中,用一行来模拟对makeFoo的调用
Run Code Online (Sandbox Code Playgroud)doReturn( mockFoo ) .when( toTest ) .makeFoo( any( A.class ), any( B.class ), any( C.class ));如果要检查传递给构造函数的参数,可以使用比any()更具体的匹配器.
如果你只是想要返回你班级的模拟对象,我认为这应该适合你.无论如何,您可以在此处阅读有关模拟对象创建的更多信息:
http://code.google.com/p/mockito/wiki/MockingObjectCreation
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)
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)
从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构造内部,所有对象构造都返回一个模拟。
| 归档时间: |
|
| 查看次数: |
204417 次 |
| 最近记录: |