2016-07-18 90 views
0

私はMockito junitテストの問題に直面しています。私はそれが初めてで、ちょっと問題と混同しています。これについての助けに感謝します。これはテストは2を使用して、私のJUnitテストコードMockitoでのJunitテストNotaMockException

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

     } 

答えて

3

ある

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

これらは、私は次の例外を取得

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

を書くことを計画していたために私のクラスです異なるモックライブラリを作成し、相互に協力させようとします。 しかし、彼らはちょうどそのようには動作しません。

問題は、コードのこの部分にある

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

オブジェクト「c」は、それがPowerMockモックオブジェクトの、Mockitoモックではありません。 これは2種類のモックライブラリです。それらのモックは協力できません。

// This piece of code uses PowerMock libriary to create a mock, 
// and this object won't work with Mockito 
c= PowerMock.createMock(C.class); 

// To use Mockito.when() method you should use Mockito mock objects 
// For example: 
c = Mockito.mock(C.class); 
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); 
    } 
}  
関連する問題