我的代码看起来怎么样?
@RunWith(PowerMockRunner.class)
@PrepareForTest({Calendar.class})
public class TestSomething {
@Test
public void mockCalendar() {
Calendar calendar = Calendar.getInstance();
PowerMockito.mockStatic(Calendar.class);
//a few attempts here
PowerMockito.when(Calendar
如何验证第二个静态方法是使用PowerMock框架在第一个静态方法中调用的?
public class A {
public static int firstMethod(String s) {
return secondMethod(s, 10);
}
public static int secondMethod(String s, Integer i){
return /*some expression*/;
}
}
更新:
我还没有看到任何使用PowerMock框架的解决方案。在中,我肯定没有任何可以接受的答案。
Upda
我正在开发一个使用Kotlin的Android项目。我还在我的项目中添加工具测试。老实说,这是我第一次编写Android工具测试,我有点纠结于此。我正在尝试使用Mokito模拟函数的行为。 请看下面的代码片段: //Create a mock object of the class Calculator
Calculator mockCalculator = Mockito.mock(Calculator.class);
//Return the value of 30 when the add method is called with the arguments
我在我的测试类中有多个模拟语句,一切都很好。我将为DAO模拟添加一个新语句,如下:
Mockito.when(myDAO.saveOrUpdate(Mockito.any())).thenReturn(Mockito.any());
But I get exception as :
org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers!
我使用了参数匹配,而不是原始值,所以我没有混合anything.What可以在这里吗?
我有一个类(PriceSetter),我正在用Mockito进行测试,这个类有一个内部依赖项(一个数据库)。我想模拟这个内部依赖项,然后将它注入类中,但是依赖项没有在构造函数中指定。因此,Mockito会自动尝试执行构造函数注入,而依赖项永远不会被注入。
我尝试在我的数据库对象上使用@Mock,在我的PriceSetter类上使用@InjectMocks,但是Mockito自动调用构造函数,由于数据库没有传递到构造函数,所以它无法注入我的数据库模拟。
class PriceSetter {
private Table priceTable;
public PriceSette
我有一节课。
Class First {
private Second second;
public First(int num, String str) {
second = new Second(str);
this.num = num;
}
... // some other methods
}
我想先为class的公共方法编写单元测试。我想避免执行类Second的构造函数。
我这样做了:
Second second = Mockito.mock(Second.class);
Mockito.when(new Seco
我试图模拟一个具有collect函数的成员来返回一个mock对象,而不是访问该函数本身。但是,不管我如何嘲弄私人成员,它都进入了这个功能。
public class BasicReportManager implements ReportManager {
private final ReportCollector reportCollector;
public BasicReportManager(String id) {
this.reportCollector = new ReportCollector(id);
}
private v
我想测试一个方法,该方法获取一个以信息为参数的对象:
//mocking a user object with some random data
User user createMockUser(loginName, firstName, lastName, salutation, company, mailAddress);
//do something with that user object
foo(user);
测试的重点是测试foo(用户)。
使用createMockUser()的这些变体:
使用when函数:
protected User createMockUser(Stri
public class Employee {
public void getAddress(){
Address add = new Address();
String a = add.addAddress();
System.out.println(a);
}
}
public class Address {
public String addAddress(){
return "Emp Address";
}
}
public class MockitoTest {