Results 1 to 2 of 2
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    10

    Default Please help me with Mock Objects.

    Please do not close this thread.

    My code:

    Java Code:
    package com.tugay.learningjunit;
    
    public class MyClass {
    	
    	RandomNumberGenerator randomNumberGenerator = new RandomNumberGenerator();
    	
    	public int shoot(){
    		int someNumber = randomNumberGenerator.generateRandomNumber();
    		return someNumber*2;
    	}
    }
    
    package com.tugay.learningjunit;
    
    import java.util.Random;
    
    public class RandomNumberGenerator {
    	public int generateRandomNumber(){
    		return new Random().nextInt(15);
    	}
    }
    I am trying to test the shoot() method. Since it generated a random integer every time, I am trying something like this:

    package com.tugay.tests;

    import junit.framework.Assert;

    import org.junit.Test;
    import org.mockito.InjectMocks;
    import org.mockito.Mock;
    import static org.mockito.Mockito.when;

    import com.tugay.learningjunit.MyClass;
    import com.tugay.learningjunit.RandomNumberGenerator;

    Java Code:
    public class MyClassTester {
    
    	@InjectMocks
    	private MyClass myObject = new MyClass();
    
    	@Mock
    	private RandomNumberGenerator randomNumberGenerator;
    
    	@Test
    	public void testShoot() {
    
    		when(randomNumberGenerator.generateRandomNumber()).thenReturn(3);
    
    		try {
    			int resultingNumber = myObject.shoot();
    			Assert.assertEquals(6, resultingNumber);
    		} catch (Exception e) {
    
    		}
    }
    }

    But I am getting an error saying:
    java.lang.NullPointerException
    at com.tugay.tests.MyClassTester.testShoot(MyClassTes ter.java:24)

    When I try with:

    Java Code:
    private RandomNumberGenerator randomNumberGenerator = new RandomNumberGenerator();
    then I get:

    org.mockito.exceptions.misusing.MissingMethodInvoc ationException:
    when() requires an argument which has to be 'a method call on a mock'.
    For example:
    when(mock.getArticles()).thenReturn(articles);

    Also, this error might show up because:
    1. you stub either of: final/private/equals()/hashCode() methods.
    Those methods *cannot* be stubbed/verified.
    2. inside when() you don't call method on mock but on some other object.
    3. the parent of the mocked class is not public.
    It is a limitation of the mock engine.


    How can I force to return generateRandomNumber() method to return 3?

    Please help. Thank you.

  2. #2
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    10

    Default Re: Please help me with Mock Objects.

    I added the line @RunWith(MockitoJUnitRunner.class) on top of public class MyClassTester and it works fine now.
    Interesting.
    Thanks anyway.

Similar Threads

  1. develop mock ups
    By karthikeyan_raju in forum NetBeans
    Replies: 0
    Last Post: 10-19-2009, 03:58 PM
  2. Mock exams for SCWCD 5
    By Enthuware in forum Java Software
    Replies: 3
    Last Post: 04-24-2009, 03:14 PM
  3. SCBCD 5.0 Mock Exam Questions
    By kks_krishna in forum Reviews / Advertising
    Replies: 0
    Last Post: 04-02-2008, 08:26 AM
  4. 800 Mock Questions for SCJP 1.6
    By Enthuware in forum Java Software
    Replies: 2
    Last Post: 01-16-2008, 11:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •