Mockito + PowerMock LinkageError while mocking system class

I’ve got such a code snippet: @RunWith(PowerMockRunner.class) @PrepareForTest({Thread.class}) public class AllMeasuresDataTest { @Before public void setUp() throws Exception { } @Test public void testGetMeasures() { AllMeasuresData measure = new AllMeasuresData(); assertEquals(measure.getMeasures(), null); HashMap<String, Measure> map = new HashMap<String, Measure>(); measure.setMeasures(map); assertEquals(measure.getMeasures(), map); measure.setMeasures(null); assertEquals(measure.getMeasures(), null); } @Test public void testAllMeasuresData() throws IOException { ClassLoader loader … Read more

How do I mock an autowired @Value field in Spring with Mockito?

I’m using Spring 3.1.4.RELEASE and Mockito 1.9.5. In my Spring class I have: @Value(“#{myProps[‘default.url’]}”) private String defaultUrl; @Value(“#{myProps[‘default.password’]}”) private String defaultrPassword; // … From my JUnit test, which I currently have set up like so: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({ “classpath:test-context.xml” }) public class MyTest { I would like to mock a value for my “defaultUrl” field. Note … Read more

Unfinished Stubbing Detected in Mockito

I am getting following exception while running the tests. I am using Mockito for mocking. The hints mentioned by Mockito library are not helping. org.mockito.exceptions.misusing.UnfinishedStubbingException: Unfinished stubbing detected here: -> at com.a.b.DomainTestFactory.myTest( E.g. thenReturn() may be missing. Examples of correct stubbing: when(mock.isOk()).thenReturn(true); when(mock.isOk()).thenThrow(exception); doThrow(exception).when(mock).someVoidMethod(); Hints: 1. missing thenReturn() 2. you are trying to stub a … Read more

Mocking python function based on input arguments

We have been using Mock for python for a while. Now, we have a situation in which we want to mock a function def foo(self, my_param): #do something here, assign something to my_result return my_result Normally, the way to mock this would be (assuming foo being part of an object) = MagicMock(return_value=”mocked!”) Even, if … Read more