Tag: mockito

如何将mock注入到具有@Transactional的@Service中

我在我的unit testing中有任何问题,我有类似的东西。 如果使用Transactional注释blargh函数,则会在blargh上覆盖模拟注入。 如果我删除了Transactional,那么模拟就会停留在那里。 通过观察代码,当服务中的函数使用transactinal进行注释时,Spring似乎懒洋洋地加载服务,但是当服务中的函数没有进行注释时,它会急切地加载服务。 这会覆盖我注入的模拟。 有一个更好的方法吗? @Component public class SomeTests { @Autowired private SomeService someService; @Test @Transactional public void test(){ FooBar fooBarMock = mock(FooBar.class); ReflectionTestUtils.setField(someService, “fooBar”, fooBarMock); } } @Service public class someService { @Autowired FooBar foobar; @Transactional // <– this causes the mocked item to be overridden public void blargh() { fooBar.doStuff(); } […]

PowerMock Mockito:如何模拟所有静态方法?

在使用PowerMock(使用Mockito)时,我们是否需要模拟类的所有静态方法? 我的意思是,假设我们有: class MockMe { public static MockMe getInstance(){ //return new Instance via complex process; } public static List anotherStaticMethod(){ // does xyz } } 我的问题是,如果我需要模拟getInstance方法,是否有必要模拟“anotherStaticMethod”? PowerMock版本:1.3,Mockito版本:1.8

ContentValues的方法没有被模拟

我正在和Mockito一起创建一个测试。 在测试中,我正在创建一个ContentValues类型的对象。 当我运行此测试时,我收到错误: java.lang.RuntimeException: Method put in android.content.ContentValues not mocked. 这是最小的代码: import android.content.ContentValues; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.class) public class MyClassTest { @Test public void test1() { ContentValues cv = new ContentValues(); cv.put(“key”, “value”); } } 该怎么做才能避免这个错误?

Springockito怎么样?

我想在我的一个IT中使用Springockito来模拟DAO bean。 在我的IT中,我必须使用spring context.xml来自动assembly某些服务,并使用mockApplication.xml来模拟DAO。 那么,我如何同时使用两个xml配置文件呢? @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(loader = SpringockitoContextLoader.class, locations = {“classpath*:/MockApplicationContext.xml”}) public class PayRollComponentFacadeIT { @Autowired IPayRollComponentFacade payRollComponentFacade; @ReplaceWithMock @Autowired IPayRollPersistenceManager payRollPersistenceManager; 我已将模拟上下文包含为@ContextConfiguration(loader = SpringockitoContextLoader.class, locations = {“classpath*:/MockApplicationContext.xml”}) 但我必须包括spring上下文@ContextConfiguration(locations = {“classpath*:/testApplicationContext.xml”}) 关心Rajib

Mockito / Powermockito模拟私有空方法

我需要使用mockito和powermock来模拟一个不使用参数的私有void方法。 该方法属于间谍的实例。 我知道我需要这样做的事实表明代码不好但我正在使用一个旧项目将unit testing从一个测试框架转换到另一个测试框架。 如果有人有任何建议,将不胜感激。 谢谢! 到目前为止,我试过这个: PowerMockito.doNothing().when(Whitebox.invokeMethod(spy,”method”,null)); 但我得到这个错误: No method found with name ‘method’ with parameter types: [ ]

PowerMockito:使用matchers模拟静态方法时得到InvalidUseOfMatchersException

当我测试这个静态方法时 public class SomeClass { public static long someMethod(Map map, String string, Long l, Log log) { … } } 同 import org.apache.commons.logging.Log; @RunWith(PowerMockRunner.class) //@PrepareForTest(SomeClass.class) public class Tests { @Test public void test() { … PowerMockito.mockStatic(SomeClass.class); Mockito.when(SomeClass.someMethod(anyMap(), anyString(), anyLong(), isA(Log.class))).thenReturn(1L); … } } 我得到了InvalidUseOfMatchersException 。 我的问题是: 当所有参数都使用匹配器时,为什么会出现此exception? 怎么解决? 我调试了它,发现isA(Log.class)返回null。 当我将@PrepareForTest批注添加到测试类并运行测试时,junit没有响应。 为什么? 编辑 我试图不使用参数匹配器,并得到了 org.mockito.exceptions.misusing.MissingMethodInvocationException:when()需要一个必须是’模拟上的方法调用’的参数。 例如:when(mock.getArticles())。thenReturn(articles); […]

测试是否调用了另一种方法

所以我确定那里有类似的东西,但我一直在寻找一个小时,并没有找到我正在寻找的东西。 说我有一个看起来像这样的课: public class MyClass { public void myMethod(boolean shouldCallOtherMethod) { if(shouldCallOtherMethod) { otherMethod(); } } public void otherMethod() { System.out.println(“Called”); } } 我如何制作这样的作品呢? @Test public void shouldCallMethod() { MyClass myClass = new MyClass(); myClass.myMethod(true) // verify myClass.otherMethod method was called }

为什么Mockito @InjectMocks可能是一个避免的事情?

为什么@InjectMocks可能是这种测试要避免的事情。 @RunWith(MockitoJUnitRunner.class) public class MyClassTest { @Mock private Bar bar; @InjectMocks private Foo foo; // created by Mockito @Test public void shouldCallMethod() { // when foo.myMethod(); // then … } } Foo.java public class Foo { private final Bar bar; public Foo(Bar bar) { this.bar = bar; } … 我在对这个答案的评论中读到了这个: https : //stackoverflow.com/a/21172873/516167 关于@InjectMocks 标记应在其上执行注射的区域。 […]

与mockito的模拟构造函数

我想将构造函数模拟为方法。 public String generaID() { GeneraIDParaEntidadCliente aux = new GeneraIDParaEntidadCliente(nombre, registro); entidad.setID(aux.generaID); } 在我的测试中,我想做这样的事情: when(new GeneraIDParaEntidadCliente(anyString(), any(Entidad.class)).thenReturn(generaIdMock) 但是给我这个错误 org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 知道为什么吗?

Mockito – 期望0匹配,1记录(InvalidUseOfMatchersException)

我正在尝试模拟一些mongo类,这样我就不需要连接(相当标准的东西)但是下面的代码给了我一些问题: when(dbCollection.find(isA(DBObject.class))).thenReturn(dbCursor); 运行这个得到我: org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 参数匹配器的使用无效! 0匹配预期,1记录: at … GridFileManagerTest.beforeClass(GridFileManagerTest.java:67) 如果匹配器与原始值组合,则可能发生此exception: //错误:someMethod(anyObject(),“raw String”); 使用匹配器时,所有参数都必须由匹配器提供。 例如: //正确: someMethod(anyObject(),eq(“by matcher”)); 有关更多信息,请参阅Matchers类的javadoc。 如果我这样做: when(dbCollection.find(mock(DBObject.class))).thenReturn(dbCursor); 它不再有这个问题。 这似乎没有实现我想要的 – 我想在使用DBObject类型的对象调用方法时返回值。 思考?