有人知道在powermock中有一种方法可以对构造函数进行单元测试并模拟构造函数调用的方法吗?
例如,我有一个这样的类;
public class myClass {
public myClass(){
myMethod();
// do other stuff
}
public void myMethod(){
// do stuff
}
}
我想要做的是为模拟myMethod()的myClass()构造函数编写一个单元测试。
如果myMethod()是静态的,这将很容易,因为我可以使用mockStaticPar
可以模拟is操作符吗?在我的代码中,我的逻辑依赖于它是什么类型的类,但是值本身作为公共接口存储在类中。
基本上,我想要做的是通过将模拟定义为IValue来返回true,但是为is Value返回true
public class Value : IValue
{
}
public interface IValue
{
}
public class Tester
{
public bool CheckIfValue(IValue value)
{
return value is Value;
}
}
[Test]
public void TestIfV
我正在编写我的第一个单元测试(使用Mockito)。我希望测试的功能结构如下:
class ClassA {
public ClassB b;
public void methodA() {
// calls a method of ClassB
b.methodB();
}
}
class ClassB {
public ClassC c;
public void methodB() {
// calls a method of ClassC
someVariable = c.met
我在为我的一个方法编写单元测试时遇到了问题。它调用来自不同类的方法,更糟糕的是,它根据服务器连接进行一些处理,所以我需要以某种方式模拟它,但是我找不到如何做到这一点的答案。我还想指出的是,被测试的方法并不接受这个不同的类对象作为参数,而是在它自己内部实例化它,所以它看起来像这样:
public class Class1 {
public MyEnum method1(String myString){
Class2 class2 = new Class2();
return class2.method2(myString);
}
}
public
我试图使用JUnit和Mockito来模拟端到端的方法(addSomething方法)。很明显,在这个方法中,我使用了其他方法。我的怀疑是:
为了模拟端到端的方法,我也需要模拟其他方法,对吗?
如何模拟addSomething方法中的方法?
我想要的一个简单例子是:
方法A
public int summing(int sum){
int A = 5;
int B = 23;
sum = SumOfIntegers(A,B);
return sum;
}
方法B
private int SumOfIntegers(int number1, int number2){
我刚开始嘲笑,我才刚刚开始使用moq。该工具声明我们可以使用moq来模拟classes without interfaces。我想知道在嘲笑时是否可以保持类的内部状态。例如:
//define class for mocking
var mock = new Mock<ILoveThisFramework>();
//define
mock.Setup(framework => framework.DownloadExists("2.0.0.0"))
.Returns(true)
.AtMostOnce();
//use mocki
请看下面的代码片段
public class FileUploadPresenter
{
private IFileRepository FileRepository
{
get { return UnityManager.Resolve<IFileRepository>(); }
}
public void LoadData(int id)
{
//How can I redirect the below ReadData call to Mocked method in the testcase ?byp
我正在测试一个类的实例方法,但它也依赖于对另一个实例方法的调用。如何模拟来自第二个实例方法的响应?
不是实际的代码,而是等效的:
public class A{
public String[][][] m1(String[][][] arg1){
ArrayList<String[]> arg2 = new ArrayList<>();
String[][][] response = m2(arg1, arg2);
if (response == null)
return arg1;
我希望你问几个问题,并征求你的意见:
我想测试我的公共方法(我使用Spring,Mockito,JUnit):
@Service
public class MyClass{
public Long getClientId(List<String> nameSurname) throws AuthorizationException {
Long operatorId;
if(...){
(... something not interesting ...)
User user = getUserByLogin
我必须测试一些SLSB的方法,它调用当前对象上的另一个方法(使用这个关键字),我需要以某种方式对它进行存根。
考虑以下简化代码:
@Local
public interface SomeService{
public int someMethod();
public int anotherMethod();
}
@Stateless()
public class SomeServiceImpl{
@EJB
private SomeDAO sDAO;
public SomeServiceImpl(){}
public SomeService
在没有记录Expectations的下面的测试用例中,我预计动态部分模拟特性将用于使用@Injectable在UnitToTest中初始化的字段A和B。但是,方法调用总是会被模拟。只有对静态部分模拟使用无效的筛选值,才能调用真正的方法:
@Service
class A {
public String doSomething() { return "doSomething"; }
public String doSomethingElse() { return "doSomethingElse"; }
}
@Service
class B {
我希望模拟支持类的静态方法,为此,我需要使用jMockit模拟正在测试的类的方法。在下面的示例中,我想模拟方法canContinue,以便始终进入if条件。我还模拟了静态方法,并验证了之后发生的一切。
public class UnitToTest {
public void execute() {
Foo foo = //
Bar bar = //
if (canContinue(foo, bar)) {
Support.runStaticMethod(f);
// Do other
是否可以使用复杂构造函数实例化类,而无需模拟或调用其构造函数?这将非常有用,因为单元测试类不需要每次向服务中添加新的依赖项时都进行更改。
模拟服务可以通过创建类的新实现来解决这个问题,并使用空方法重写,但这不是实现的实例。这是一个问题,因为每当调用模拟服务中的方法时,都必须告诉Mockito调用真正的方法。相反,最好是实际实现服务。
例如:
class ComplexService {
private Service service1;
private Service service2;
private Service service3;
private Ser
有没有办法用PHPUnit创建一个模拟类,然后我可以使用它的类名创建一个新的实例?
我有一个接口,它定义了两个方法。类似于:
interface FooInterface {
function getA();
function getB();
}
然后我有了另一个类,它接受一个类名,创建一个该类的实例,检查它是否是它期望的实例(FooInterface),然后在该类上调用两个方法来获取一些信息。
class FooInfo {
protected $a;
protected $b;
public function __construct($fooCla
我想测试一些使用调用kotlin.system.exitProcess()的第三方代码的代码,在标准库中定义如下:
@kotlin.internal.InlineOnly
public inline fun exitProcess(status: Int): Nothing {
System.exit(status)
throw RuntimeException("System.exit returned normally, while it was supposed to halt JVM.")
}
当exitProcess()被调用时,JVM就停止了,进一步
我正在使用Retrofit并通过Dagger2注入RestAdapter和WebService类/接口
在测试中,我将我的web服务类模拟为MockWebService200 implements WebService,并从Retrofit获得此异常
java.lang.IllegalArgumentException: Only interface endpoint definitions are supported.
at retrofit.Utils.validateServiceClass(Utils.java:102)
at retrofit.Rest
我有一个带有getDefaultLanguage()方法的服务getDefaultLanguage;
我想以这样的方式在一个类ISOLanguageServiceMock中模拟这个服务:
public class ISOLanguageServiceMock {
@Mock
private ISOLanguageService isoLanguageService;
public ISOLanguageServiceMock() {
MockitoAnnotations.initMocks(this);
ISOLanguage i