标签: jmockit

Mockito与JMockit之间的比较 - 为什么Mockito投票比JMockit更好?

我正在调查哪个模拟框架用于我的项目,并将其缩小到JMockitMockito.

我注意到MockitoStackoverflow上被评为" Java最好的模拟框架 ".
在比较JMockit的" Mocking Tool Comparision Matrix "的功能时,似乎JMockit具有多种不同的功能.

有没有人有任何关于Mockito可以用JMockit无法实现的具体信息(不是意见),反之亦然?

java unit-testing jmockit mocking mockito

121
推荐指数
3
解决办法
6万
查看次数

单元测试中的计数方法调用

在单元测试中计算方法调用的最佳方法是什么.任何测试框架都允许这样做吗?

unit-testing jmockit junit4 mockito

45
推荐指数
6
解决办法
5万
查看次数

使用mockito或Jmockit模拟私有静态最终字段

我在我的类中使用私有静态最终LOGGER字段,我希望LOGGER.isInfoEnabled()方法返回false.如何使用mockito或jMockit模拟静态final字段

我的班级是:

  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;

  public class Class1 {

  private static final Logger LOGGER = LoggerFactory.getLogger(Class1.class);

    public boolean demoMethod() {
       System.out.println("Demo started");
       if (LOGGER.isInfoEnabled()) {
         System.out.println("@@@@@@@@@@@@@@ ------- info is enabled");
       } else {
         System.out.println("info is disabled");
       }
       return LOGGER.isInfoEnabled();
    }
  }
Run Code Online (Sandbox Code Playgroud)

它的junit是:

import mockit.Mocked;
import mockit.NonStrictExpectations;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static org.testng.Assert.*;
import com.source.Class1;

public class MyTest {

  @InjectMocks
  Class1 cls1;

  @BeforeMethod
  public void initMocks() { …
Run Code Online (Sandbox Code Playgroud)

java junit jmockit static-members mockito

39
推荐指数
2
解决办法
5万
查看次数

JMockit - 初始化问题

当我使用以下测试时,我得到一个警告:

警告:JMockit已按需初始化,这可能导致某些测试失败; 请查看文档以获得初始化的更好方法.

这是我的测试实现:

package test;

import static mockit.Mockit.*;
import junit.framework.TestCase;
import mockit.*;
import mockit.integration.junit4.*;


import org.junit.*;
import org.junit.runner.*;

import filip.ClassUnderTest;
import filip.LowerClass;

@RunWith(JMockit.class)
public class MockTest extends TestCase {

    @MockClass(realClass = LowerClass.class)
    public static class LowerClassMock {
        @Mock(invocations = 1)
        public String doWork() {
            return "Mockowanie dziala :D";
        }
    }

    @Before
    public void setUp() { setUpMocks(LowerClassMock.class); }

    @After
    public void tearDown() { tearDownMocks(); }

    @Test
    public void testJMockit() {
        ClassUnderTest classUnderTest = new ClassUnderTest();

        classUnderTest.print();
    }

}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

java jmockit

30
推荐指数
2
解决办法
4万
查看次数

jmockit:Attach J的本机库在此JRE错误中不可用

我试图使用jmockit对我的项目进行单元测试并得到以下错误:

java.lang.UnsatisfiedLinkError: no attach in java.library.path
java.lang.IllegalStateException: Native library for Attach API not available in this JRE
    at mockit.internal.startup.JDK6AgentLoader.getVirtualMachineImplementationFromEmbeddedOnes(JDK6AgentLoader.java:95)
    at mockit.internal.startup.JDK6AgentLoader.loadAgent(JDK6AgentLoader.java:54)
    at mockit.internal.startup.AgentInitialization.initializeAccordingToJDKVersion(AgentInitialization.java:21)
    at mockit.internal.startup.Startup.initializeIfNeeded(Startup.java:98)
    at mockit.internal.startup.Startup.initializeIfPossible(Startup.java:112)
    at org.junit.runner.Runner.<clinit>(Runner.java:22)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.UnsatisfiedLinkError: no attach in java.library.path
Run Code Online (Sandbox Code Playgroud)

我分别在类路径中包含了jdk6/lib/tools.jar,jmockit.jar和junit.jar.任何线索为什么会发生这种情况?

jmockit

22
推荐指数
4
解决办法
2万
查看次数

JMockit Expectation API:如何在方法/构造函数调用时抛出异常

在使用JMockit时,我想在构造函数调用上抛出异常,如下所示:

new Expectations(){
        {
           new FirefoxDriver();//Want to throw IllegalStateException here but how?
        }
};
Run Code Online (Sandbox Code Playgroud)

jmockit mocking expectations

20
推荐指数
1
解决办法
2万
查看次数

如何从JMockit模拟静态方法

我有一个静态方法,它将从类中的测试方法调用,如下所示

public class MyClass
{
   private static boolean mockMethod( String input )
    {
       boolean value;
       //do something to value
       return value; 
    }

    public static boolean methodToTest()
    {
       boolean getVal = mockMethod( "input" );
       //do something to getVal
       return getVal; 
    }
}
Run Code Online (Sandbox Code Playgroud)

我想通过模拟mockMethod为方法methodToTest编写一个测试用例.试图吼叫,它没有任何输出

@Before
public void init()
{
    Mockit.setUpMock( MyClass.class, MyClassMocked.class );
}

public static class MyClassMocked extends MockUp<MyClass>
{
    @Mock
    private static boolean mockMethod( String input )
    {
        return true;
    }
}

@Test
public void testMethodToTest() …
Run Code Online (Sandbox Code Playgroud)

java testing unit-testing jmockit

19
推荐指数
3
解决办法
4万
查看次数

JMockit有任何缺点吗?

这个比较表明,JMockit比其他框架有几个优点.

其他一个(JMock,EasyMock,Mockito,Unitils,PowerMock + Mockito/EasyMock)是否还有JMockit的优势?

java unit-testing jmockit jmock mockito

16
推荐指数
2
解决办法
5594
查看次数

通过JMockit调用私有方法来测试结果

我使用JMockit 1.1,我想做的就是调用私有方法并测试返回值.但是,我无法从JMockit De-Encapsulation示例中了解如何执行此操作.

我试图测试的方法是这个类中的私有方法:

public class StringToTransaction {
   private List<String> parseTransactionString(final String input) {
      // .. processing
      return resultList;
   }
}
Run Code Online (Sandbox Code Playgroud)

我的测试代码如下.

@Test
public void testParsingForCommas() {
   final StringToTransaction tested = new StringToTransaction();
   final List<String> expected = new ArrayList<String>();
   // Add expected strings list here..
   new Expectations() {
      {
         invoke(tested, "parseTransactionString", "blah blah");
         returns(expected);
      }
   };
}
Run Code Online (Sandbox Code Playgroud)

而我得到的错误是:

java.lang.IllegalStateException:此时缺少对mocked类型的调用; 请确保仅在声明合适的模拟字段或参数后才会出现此类调用

也许我在这里误解了整个API,因为我认为我不想模拟这个类..只是测试调用私有方法的结果.

java junit jmockit private-methods

14
推荐指数
1
解决办法
3万
查看次数

在正在测试的同一个类中模拟私有方法

我有一个名为的Java类MyClass,我想用JUnit进行测试.methodA我要测试的公共方法调用私有方法methodB,在同一个类中确定要遵循的条件路径.我的目标是为不同的路径编写JUnit测试methodA.此外,methodB调用服务,所以我不希望它在运行JUnit测试时实际执行.

模拟methodB和控制其返回的最佳方法是什么,以便我可以测试'methodA'的不同路径?

我更喜欢在编写模拟时使用JMockit,所以我对任何适用于JMockit的答案都特别感兴趣.

这是我的示例类:

public class MyClass  {

    public String methodA(CustomObject object1, CustomObject object2)  {

        if(methodB(object1, object2))  {
            // Do something.
            return "Result";
        }

        // Do something different.
        return "Different Result";

    }

    private boolean methodB(CustomObject custObject1, CustomObject custObject2)  {

        /* For the sake of this example, assume the CustomObject.getSomething()
         * method makes a service call and therefore is placed in this separate
         * method so that later an …
Run Code Online (Sandbox Code Playgroud)

java junit unit-testing jmockit mocking

14
推荐指数
2
解决办法
4万
查看次数