BecauseKing
BecauseKing

Reputation: 9

Junit test with Mockito NotaMockException

I am facing an issue with Mockito junit testing. I am new to it and am a bit confused with problem. Any help on this would be appreciated.

These are my class for which i am planning to write

public class B extends QuartzJobBean {

private B b;
        @Override
        protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
                try {
                b.expireContract();
            } catch (BusinessServiceException e) {
                LOGGER.error("BusinessServiceException in B : " +
                    e.getMessage());
                LOGGER.debug("BusinessServiceException in B : ", e);
            } catch (Exception e) {
                LOGGER.error("Exception in B : " +
                    e.getMessage());
                LOGGER.debug("Exception in B : ", e);
            }
        }



public class C {

@Autowired
        private D d;
        public boolean expireTime() throws BusinessServiceException
        {

            try {
                contractBusinessService.expireContract();
                return true;
            } catch (Exception e)
            {
                 e.getMessage();
             return false;
            }
        }

Getting following exception :

*org.mockito.exceptions.misusing.NotAMockException: 
 Argument passed to when() is not a mock!*

this is my junit test code

class BTest(){

  B b;

@Before
        public void setUp() {
         b= Mockito.spy(new B());
         c= PowerMock.createMock(C.class);
         b.setC(c);
        }

     @Test
        public void testExpireContract() {
           Mockito.doNothing().when(c).expireTime();

            try {
            b.executeInternal(j);


            fail("BusinessServiceException should have thrown.");
            } catch (Exception wse) {
                assertEquals("BusinessServiceException should be same.", "BusinessServiceException", wse.getMessage());
            }
            verify(b).expireTime();
            Assert.assertNotNull("value should not be null.", b);

        }

Upvotes: 0

Views: 5095

Answers (2)

alex.m
alex.m

Reputation: 814

The test uses two different mock libraries The problem is here c= PowerMock.createMock(C.class);

You trying to use c object (a PowerMock mock objest) as if it was a Mockito mock and that's why you getting the exception.

// Instead of PowerMock just use Mockito to create a mock
c = Mockito.mock(C.class);

// Then you can pass it to `Mockito.when()` method as an argument
Mockito.doNothing().when(c).expireTime();

Actually, it's possible to use PowerMock along with Mockito, but more configuration needed. Using PowerMock with Mockito offers additional advanced features, like mocking static methods.

Upvotes: 3

John
John

Reputation: 1

public class BTest {
    private C c;
    private B b;
    private JobExecutionContext j;

    @Before
    public void setUp() {
        b= Mockito.mock(B.class);
        c= Mockito.mock(C.class);
        b.setC(c);
    }

    @Test
    public void testExpireTime() {
        try {
             Mockito.doReturn(true).when(b).expireTime();
             fail("BusinessServiceException should have thrown.");
        } catch (Exception wse) {
             assertEquals("BusinessServiceException should be same.", "BusinessServiceException", wse.getMessage());
        }
        verify(b);
        Assert.assertNotNull("value should not be null.", b);
    }
}    

Upvotes: 0

Related Questions