mockito « Tools « Java Testing Q&A

Home
Java Testing Q&A
1.Development
2.FindBugs
3.HTMLUnit
4.hudson
5.junit
6.performance
7.plugin
8.profile
9.selenium
10.Tools
11.unit test
Java Testing Q&A » Tools » mockito 

1. Use Mockito to verify that nothing is called after a method    stackoverflow.com

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 ...

2. How to mock a String using mockito?    stackoverflow.com

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 ...

3. Using Mockito to test abstract classes    stackoverflow.com

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 ...

4. learning resources for mockito please    stackoverflow.com

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 ...

5. How to make mock to void methods with mockito    stackoverflow.com

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 ...

6. Mockito: How to easily stub a method without mocking all parameters    stackoverflow.com

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 ...

7. Does JMockit have any drawbacks at all?    stackoverflow.com

This comparison shows, that JMockit has several advantages over other frameworks. Are there also any advantages that one of the others (JMock, EasyMock, Mockito, Unitils, PowerMock + Mockito/EasyMock) has over ...

8. Problem with Mockito - exceptions.verification.WantedButNotInvoked    stackoverflow.com

When I put a "VerificationModeFactory.times(2)" in test before, when I run all tests of the class appears this exception: org.mockito.exceptions.verification.WantedButNotInvoked: Wanted but not invoked: serviceService.getServices(); If I run each test separately or remove "VerificationModeFactory.times(2)" ...

9. How does mockito create an instance of the mock object    stackoverflow.com

When i create a mock object of say class Employee. It doesnt call the constructor of Employee object. I know internally Mockito uses CGLIb and reflection, creates a proxy class that ...

10. Problem with ArgumentCaptor and consecutive call to same methods (bug or feature?)    stackoverflow.com

I'm having a problem with ArgumentCaptor not being able to record the arguments when calling the same method a number of times. Basically this does not seem to work:

List<Dummy> mList ...

11. Capture an argument in Mockito    stackoverflow.com

I'm testing a certain class. This class is internally instantiating a "GetMethod" object that gets passed to a "HttpClient" object that gets injected into the tested class. I'm mocking the "HttpClient" class, ...

12. Is it a right case for Mockito spy?    stackoverflow.com

Let's say I have a class

class SomeClass
{
  public void methodA()
  {}

  public void methodB()
  {}

  public void someMethod()
  {
     methodA();
  ...

13. Final method mocking    stackoverflow.com

I need mock some class with final method using mockito. I have wrote something like this

@Test
public void test() {
    B b = mock(B.class);
    doReturn("bar ...

14. How to check if a parameter contains two substrings using Mockito?    stackoverflow.com

I have a line in my test that currently looks like:

Mockito.verify(mockMyObject).myMethod(Mockito.contains("apple"));
I would like to modify it to check if the parameter contains both "apple" and "banana". How would I go ...

15. Comparison between Mockito vs JMockit - why is Mockito voted better than JMockit?    stackoverflow.com

Hello
I'm investigating which mocking framework to use for my project and have narrowed it down to JMockit and Mockito. I notice that Mockito was voted "the ...

16. Unit-testing with mockito (partial mocking)    stackoverflow.com

I have a problem with Mockito. Is it possible to do such a thing:

ClassX x = mock(ClassX.class)
when(x.methodB()).thenReturn("toto");
String result = x.methodA();
I'm working with Mockito 1.7. I saw there was a "spy" system but they ...

17. Mockito complains about wrong arguments    stackoverflow.com

We try to verify the behaviour of an action with Mockito. The test code looks like this

final Type1 mock = mock(Type1.class);
new SomeAction<Type1>(mock).actionPerformed(null);

verify(mock).someMethod();
The method actionPerformed contains just the call of someMethod on ...

18. Test login three times with mockito throws an Exception!    stackoverflow.com

I have a test but i am not quite good with it, i want to verify that when the methods login is called three times with wrong username and password it ...

19. Mockito conditional verification with factories    stackoverflow.com

Is it possible in mockito to verify a method was called on a mock based on whether the mock was actually used in the unit-under-test? For a simple example, I supply a ...

20. Why does my Mockito mock object use real the implementation    stackoverflow.com

I had an issue with mocking Apache Http client. The following attempt to create a mock:

DefaultHttpClient httpClient = Mockito.mock(DefaultHttpClient.class);
Fails to create a true mock. The above row gets executed without ...

21. Alternatives to partial mocking?    stackoverflow.com

Tired of hand-crafting mocks, I am trying to introduce Mockito to my project. Suppose I have a bunch of database accessors which isolate all Hibernate queries for given use case (or service). ...

22. Mockito is not mocking a call    stackoverflow.com

One of the functions I'm testing is sshing into a machine. I want to mock the ping method, that actually tries to ssh into a machine, since I am not really ...

23. Mockito to test void methods    stackoverflow.com

I have following code I want to test:

public class MessageService {
    private MessageDAO dao;

    public void acceptFromOffice(Message message) {
       ...

24. Nested mocking in Mockito    stackoverflow.com

I have this Mockito code:

interface Dao {
    public void doSomething();
}

class LegacyClass {
    Dao dao;

    public String legacyMethod() {
     ...

25. Verify whether one of three methods is invoked with mockito    stackoverflow.com

I have three methods like these ones:

public void method1(String str){
    ...
}

public void method1(String str, String str2, String str3){
    ...
}

public void method1(String str, String str2, Object[] ...

26. Throwing exceptions from Mockito mock    stackoverflow.com

Something tells me I'm doing something wrong...it shouldn't be this hard. I have a class that relies on some inner class. I'm creating that inner through a protected method so that I ...

27. Is this possible in Mockito: when(SomeClass.getIntance()).thenReturn(myMock)?    stackoverflow.com

I'm working with some legacy code, where I have to implement a new Handler. And in this handler, there's an object which is unfortunately initialized by the framework using some hard-coded ...

28. How to test a method invocation inside an anonymous class using mockito    stackoverflow.com

@Transactional(propagation = Propagation.REQUIRED)
    public void exe() {
        try {
            Reserve ...

29. Can Mockito stub a method that without regard to the argument?    stackoverflow.com

I'm trying to test some legacy code, using Mockito. I want to stub a FooDao that is used in production as follows:

foo = fooDao.getBar(new Bazoo());
I can write:
when(fooDao.getBar(new Bazoo())).thenReturn(myFoo);
But the obvious problem is ...

30. Can Mockito capture arguments of a method called multiple times?    stackoverflow.com

I have a method that gets called twice, and I want to capture the argument of the second method call. Here's what I've tried:

ArgumentCaptor<Foo> firstFooCaptor = ArgumentCaptor.forClass(Foo.class);
ArgumentCaptor<Foo> secondFooCaptor = ArgumentCaptor.forClass(Foo.class);
verify(mockBar).doSomething(firstFooCaptor.capture());
verify(mockBar).doSomething(secondFooCaptor.capture());
// then do ...

31. Testing a passed in value.. not just the returned value    stackoverflow.com

So I have a class that looks like this.

@Inject
AnotherClass anotherClass;

public class Foo {
    public Boolean someMethod(){
         Holder<Boolean> booleanHolder = new ...

32. mockito: how to verify calls on real implementation?    stackoverflow.com

I'd like to verify calls to a logger object, so that the real implementation is still called (so I can see the output during tests). Something like

verify(logger).error(anyString())

33. How to test a scenario that calls System.exit() in Java?    stackoverflow.com

Possible Duplicate:
Java: How to test methods that call System.exit()?
In a certain scenario, I want to test that the application makes a function call to ...

34. Testing Java enhanced for behavior with Mockito    stackoverflow.com

I want to test a java method that has an enhanced for on it using Mockito. The problem is that when I don't know how to set the expectations for the ...

35. How to mock/test method that returns void, possibly in Mockito    stackoverflow.com

I came across a problem and I can't find an elegant solution. So the problem is with a mock of Selenium web driver, and I dont know how should I test/mock void ...

36. Dynamicaly changing behavior of mocks in Mockito    stackoverflow.com

Hello I would like to know, if there is any way to dynamicaly modify behaviour of mocks in Mockito. For example I have a method count() and I would like Mockito to ...

37. Mockito Disregard Parameters    stackoverflow.com

Is there a way to get a mocked class to return some object no matter what arguments the function is called with? For example, if one of my parameters' types did not ...

38. Does mockito have an equivelent idiom to jMock's States?    stackoverflow.com

The book Growing Object Oriented Software gives several examples in jMock where the state is made explicit without exposing it through an API. I really like this idea. Is there a ...

39. How to mock a void static method to throw exception with Powermock?    stackoverflow.com

I am trying to use Powermock and Mockito to mock a void static method to throw exception as below. But I met a problem. Unless I make the two invocations of ...

40. Getting Mockito and Powermock to throw error correctly    stackoverflow.com

I have the following code

@PrepareForTest({Mongo.class, XYMongo.class, DB.class})
public class XYMongoTest extends UnitTest{

String host = Play.configuration.getProperty("mongo.host");
int port = Integer.parseInt(Play.configuration.getProperty("mongo.port"));  
String name = Play.configuration.getProperty("mongo.name");

@Test
public void testRetrieveMongoDBSuccessful() throws UnknownHostException, MongoException, Exception
{
    ...

41. How to test that a method was called with the correct parameter?    stackoverflow.com

My app has two classes, FireWatcher and AlarmBell. When a fire starts, the watcher should ring the bell, with a level. For small fires, ring the bell with a small alarm ...

42. Is it possible to do strict mocks with Mockito?    stackoverflow.com

I'd like to use strict mocks, at least when developing for the first time some tests against old code, so any methods invoked on my mock will throw an exception if ...

43. How do I call Apache's MethodUtils methods with mocks from Mockito as paramaters?    stackoverflow.com

I use org.apache.commons.beanutils.MethodUtils (api link) on occasion. I generally avoid reflection, but alas this case called for it. My problem comes to testing. I use Mockito ...

44. mockito : how to unmock a method?    stackoverflow.com

I have a JUnit class with different methods to perform different tests. I use Mockito to create a spy on real instance, and then override some method which is not relevant to ...

45. stubbing methods that manipulates parameters with mockito    stackoverflow.com

I have the following situation:

class Worker {  
  public Integer somework(){  
  Integer k=0;  
  Helper h= new Helper();  
 ...

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.