使用Mockito NotaMockException进行Junit测试

时间:2016-07-18 01:12:08

标签: java junit

我正面临着Mockito junit测试的问题。我是新手,对问题有点困惑。任何有关这方面的帮助将不胜感激。

这些是我计划编写的课程

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;
            }
        }

获得以下例外:

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

这是我的junit测试代码

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);

        }

2 个答案:

答案 0 :(得分:3)

该测试使用两个不同的模拟库 问题出在c= PowerMock.createMock(C.class);

你试图使用c对象(一个PowerMock模拟对象),好像它是一个Mockito模拟,这就是你得到异常的原因。

// 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();

实际上,可以use PowerMock along with Mockito,但需要更多配置。将PowerMock与Mockito一起使用可提供其他高级功能,例如模拟静态方法。

答案 1 :(得分:0)

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);
    }
}