简体   繁体   English

使用Mockito NotaMockException进行Junit测试

[英]Junit test with Mockito NotaMockException

I am facing an issue with Mockito junit testing. 我在Mockito junit测试中遇到问题。 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 这是我的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);

        }

The test uses two different mock libraries The problem is here c= PowerMock.createMock(C.class); 该测试使用两个不同的模拟库。问题出在这里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. 您试图使用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();

Actually, it's possible to use PowerMock along with Mockito , but more configuration needed. 实际上,可以将PowerMock与Mockito一起使用 ,但需要更多配置。 Using PowerMock with Mockito offers additional advanced features, like mocking static methods. 将PowerMock与Mockito一起使用可提供其他高级功能,例如模拟静态方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM