mockito

Use Mockito to verify that nothing is called after a method

I'm using Mockito to write a unit test in Java, and I'd like to verify that a certain method is the last one called on an object. I'm doing something like this in the code under test: row.setSomething(value); row.setSomethingElse(anotherValue); row.editABunchMoreStuff(); row.saveToDatabase(); In my mock, I don't care about the order ...

How does "static reflection" work in java? (ex. in mockito or easymock)

I'm a .NET guy - and I mainly code in C#. Since C# 3.0, we can leverage lambda expressions and expression trees to use static reflection. For example, it is possible to implement GetMethodName in the following snippet to return the name of the method passed in parameter: string methodName = GetMethodName( o => o.DoSomething()); Console...

Strategies to mock a webservice

I'm implementing a client consuming a webservice. I want to reduce dependencies and decided to mock the webservice. I use mockito, it has the advantage vs. EasyMock to be able to mock classes, not just interfaces. But that's not the point. In my test, I've got this code: // Mock the required objects Document mDocument = mock(Document.c...

How to mock a String using mockito?

I need to simulate a test scenario in which I call the getBytes() method of a String object and I get an UnsupportedEncodingException. I have tried to achieve that using the following code: String nonEncodedString = mock(String.class); when(nonEncodedString.getBytes(anyString())).thenThrow(new UnsupportedEncodingException("Parsing erro...

Using Mockito to test abstract classes

I'd like to test an abstract class. Sure, I can manually write a mock that inherits from the class. Can I do this using a mocking framework (I'm using Mockito) instead of hand-crafting my mock? How? ...

learning resources for mockito please..

i am required to use mockito to create unit testing framework for existing code. I am unable to find a good place to get started with learning Mockito. Could you please point me to a good learning resource for mockito? (online resource or otherwise) ...

GWT Mockito integration

I'm trying to set up and use Mockito into a GWT project, and I'm having trouble using it on the client side (in javascript). I tried to add a module and include Mockito, but it seems not to work (lots of errors). I also tried to do a full checkout from svn and integrate GWT in it that way, the same errors. How should this be done? Thanks...

MockEJB - JUnit Mockito - cannot rebind a mock EJB on second unit test

Hi I have a question related to MockEJB. I need to write unit tests to test a code that is calling a EJB. I use Mockito to write a mock for the EJB, and MockEJB to simulate the JNDI context. My tests look like this : @Test public void test1() throws Exception { // create a mock instance NetworkManager aMockManager = createMockNet...

Using Mockito to mock classes with generic parameters

Is there a clean method of mocking a class with generic parameters? Say I have to mock a class Foo<T> which I need to pass into a method that expects a Foo<Bar>. I can do the following easily enough: Foo mockFoo = mock(Foo.class); when(mockFoo.getValue).thenReturn(new Bar()); Assuming getValue() returns the generic type T. But that...

Java problem - getting errors related to Mockito

Hi, I'm using the Mockito library for Java testing and getting errors in Mockito when I run my test. (I'm using the NetBeans IDE, in case that's relevant). For example, I have a class called Animal and I am trying to perform the following simple test: @Test public void mokito_test(){ Animal mockAnimal = mock(Animal.class); An...

Using Mockito's generic "any()" method

I have an interface with a method that expects an array of Foo: public interface IBar { void DoStuff(Foo[] arr); } I am mocking this interface using Mockito, and I'd like to assert that DoStuff() is called, but I don't want to validate what argument are passed - "don't care". How do I write the following code using any(), the gener...

Running Junit & PowerMock with Mockito through PowerMockRunner from maven

Hi, I am not being able to run Powermock through maven. I'm the PowerMock Mockito and PowerMockRunner for driving a jUnit test. Here's the test: @RunWith(PowerMockRunner.class) @PrepareForTest( { UserLocalServiceUtil.class, ExpandoBridge.class }) public class AlertNotificationsTest { //... I haven't configured anyting special for ru...

Java: Mock testing probably with Mockito

I think I'm not using verify correctly. Here is the test: @Mock GameMaster mockGM; Player pWithMock; @Before public void setUpPlayer() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { pWithMock = new Player(mockGM); } @Test public void mockDump() { pWithMock.testDump(); ...

What to do when Java Best Practices conflict with Mockito

My development team has started to use Mockito and have classes that have been defined as 'final'. I've read in Effective Java by Joshua Bloch and in the SO thread When to use final that all classes should use the final modifier. There have been some disagreement in the thread, but I do agree with the idea of forcing class composition ...

How to make mock to void methods with mockito

How to make mock void methods.I performed observer pattern but i cant mock with mockito because i dont know the mock way with mockito.And i search from the internet i cant find properly sample. My class looks like public class World{ List<Listener> listeners; void addListener(Listener item) { listeners.add(item); } void doActio...

Mockito: How to test my Service with mocking?

Hi, I'm new to mock testing. I want to test my Service method CorrectionService.CorrectPerson(Long personId). The implementation is not yet written but this it what it will do: CorrectionService will call a method of AddressDAO that will remove some of the Adress that a Person has. One Person has Many Addresses I'm not sure what the ...

Mockito: How to easily stub a method without mocking all parameters

Hi, I have a method i'd like to stub but it has a lot of parameters. How can i avoid mocking all parameters but still stub the method. Ex: //Method to stub public void myMethod(Bar bar, Foo foo, FooBar fooBar, BarFoo barFoo, .....endless list of parameters..); ...

Injecting Mockito mocks into a Spring bean

I would like to inject a Mockito mock object into a Spring (3+) bean for the purposes of unit testing with JUnit. My bean dependencies are currently injected by using the @Autowired annotation on private member fields. I have considered using ReflectionTestUtils.setField but the bean instance that I wish to inject is actually a proxy an...

Using Mokito, how do I match against the key-value pair of a map?

I am need to send a return specific value from a mock object based on a specific key value. concreate class: map.put("xpath", "PRICE"); search(map); Test case: IOurXMLDocument mock = mock(IOurXMLDocument.class); when(mock.search(.....need help here).thenReturn("$100.00"); how do I mock this method call for this key value pair? ...

How to properly match varargs in Mockito

I've been trying to get to mock a method with vararg parameters using Mockito: interface A { B b(int x, int y, C... c); } A a = mock(A.class); B b = mock(B.class); when(a.b(anyInt(), anyInt(), any(C[].class))).thenReturn(b); assertEquals(b, a.b(1, 2)); This doesn't work, however if I do this instead: when(a.b(anyInt(), anyInt()))...