jmockit

Is there a way in JMockit to call the original method from a mocked method?

試著忘記壹切 提交于 2019-12-18 04:59:15
问题 In my mock class, I'm mocking method foo(). For some test cases, I want the mock implementation of foo() to return a special value. For other test cases, I want to use the real implementation of foo(). I have a boolean defined in my mock class so that I can determine in the mock method whether I want to return the special value, or use the "real" method. The problem is, I can't seem to figure out how to call the real method from the mocked method. I found that you can define a special member

Using JMockit to mock autowired interface implementations

爷,独闯天下 提交于 2019-12-18 04:40:45
问题 We are writing JUnit tests for a class that uses Spring autowiring to inject a dependency which is some instance of an interface. Since the class under test never explicitly instantiates the dependency or has it passed in a constructor, it appears that JMockit doesn't feel obligated to instantiate it either. Up until now we have been using SpringRunner to have Spring load mock dependencies for us, which works. Two things we don't like about this are 1) the Spring framework has to be loaded

can jmockit and robolectric coexist?

那年仲夏 提交于 2019-12-13 11:49:27
问题 I'm trying to implement a unit test using Robolectric to replace the stubbed methods in android.jar while also using jMockit to mock an Android class (Fragment, in my case). However, I can't seem to get it to work. If I annotate the test class with @RunWith(RobolectricTestRunner.class) , I get: java.lang.IllegalStateException: JMockit wasn't properly initialized; check that jmockit.jar precedes junit.jar in the classpath (if using JUnit; if not, check the documentation) If I use @RunWith

How to mock a method in an ENUM class?

北城以北 提交于 2019-12-12 10:35:30
问题 I am working on writing JUNIT test case for my below ENUm class. My below class will only give me the hostname for the current machine where I am running my code. While I am writing JUNIT test, how can I mock the below class, so that I can change getHostName() method whenever I want to so that whenever I am calling getDatacenter() , it can return me whatever hostname I am passing by mocking it. I don't want to make it as a parametrized. I just want to test certain cases while changing the

Counting method invocations in Unit tests

淺唱寂寞╮ 提交于 2019-12-12 08:17:15
问题 What is the best way to count method invocations in a Unit Test. Do any of the testing frameworks allow that? 回答1: It sounds like you may want to be using the .expects(1) type methods that mock frameworks usually provide. Using mockito, if you were testing a List and wanted to verify that clear was called 3 times and add was called at least once with these parameter you do the following: List mock = mock(List.class); someCodeThatInteractsWithMock(); verify(mock, times(3)).clear(); verify(mock

How to use Verifications on an un-mockable type in JMockit?

会有一股神秘感。 提交于 2019-12-12 05:20:09
问题 In JMockit, there are types that are outright un-mockable (like java.lang.Class) or are really bad ideas to mock (like java.lang.Math, as stated in this question). How do you use Verifications on these types? Using the linked question as an example, how would I verify that Math.pow() got called with the right arguments? By way of example, this test passes even though I call Math.pow() but I check for Math.min() in the Verifications - how could I make this fail? public class TestTest { public

Null Pointer Exception in mocking Interface method

和自甴很熟 提交于 2019-12-12 00:58:34
问题 I'm using TestNG for testing and JMockit for mocking mockMethod(). Here's the test case: @Test public void testClass1() { new MockUp<MyInterface>() { @Mock public int myMethod(final MyObject someObject ){ return 0; } }; MyObject obj = new MyObject(); Assert.assertEquals(obj.mockMethod(someObject),0); } } The mockMethod() I'm calling for assertEquals() looks like this: public class Class1 { MyInterface my; public int mockMethod(final MyObject someObject ){ ...... //processing steps return my

JMockit - two mocked instances of the same type

我怕爱的太早我们不能终老 提交于 2019-12-11 23:39:28
问题 I'm using the JMockit framework and I'm trying to test my simple EventBus implementation which allows EventHandlers to be registered for Event types. When an event is fired on the event bus, all registered handlers get notified. An event can be consumed by an event handler which will cause that subsequent handlers will NOT be notified of the event. My test method looks like this: // The parameter secondHandler should be mocked automatically by passing it // as an argument to the test method

How to increase the conditional coverage using jmockit for cobertura?

怎甘沉沦 提交于 2019-12-11 19:46:31
问题 I am trying to increase the code coverage for following below method using jmockit - Below is my method in DataLogger class for which I am trying to increase the coverage - public void logDebug(final Object... objects) { if (m_logger.isDebugEnabled() && objects != null) { m_logger.debug(message(objects)); } } And below is my isDebugEnabled - public boolean isDebugEnabled() { return m_logger.isDebugEnabled(); } Somehow my cobertura coverage report is showing as Conditional Coverage 25% [1/4]

java.lang.NoSuchMethodException with JMockit 1.5 and Robolectric 2.2

老子叫甜甜 提交于 2019-12-11 18:12:32
问题 I'm trying to use JMockit 1.5 with robolectric 2.2 but I get a java.lang.NoSuchMethodException as soon I try to create an Activity. I've reduced the test to this: @RunWith(RobolectricTestRunner.class) //@RunWith(MyTestRunner.class) public class Test_login { @Test public void test_login(@Mocked final Intent in) { FragmentActivity frag = Robolectric.buildActivity(MainActivity.class).create().get(); } } But I get this stack trace: java.lang.RuntimeException: java.lang.NoSuchMethodException: org