Mokito doesn't recognize my mocking class when passing through ()

I want the closeSession() method to closeSession() exception when it is called, so that I can verify that my log has been executed. I mocked the Crypto object as mockCrypto and set it like this:

 @Test public void testdecrpyMcpDataExceptions() throws Exception { Crypto mockCrypto = Mockito.mock(Crypto.class); try { mockCrypto = CryptoManager.getCrypto(); logger.info("Cyrpto Initialized"); } finally { logger.info("Finally"); try { logger.info("About to close Crypto!"); Mockito.doThrow(new CryptoException("")).when(mockCrypto).closeSession(); mockCrypto.closeSession(); } catch (CryptoException e) { logger.warn("CryptoException occurred when closing crypto session at decryptMcpData() in CipherUtil : esi"); } } } 

However, when I start, I get an error message:

 Argument passed to when() is not a mock! 

Am I mocking the class or am I just missing something?

+4
source share
2 answers

Do not rewrite your layout on

 mockCrypto = CryptoManager.getCrypto(); 

Tested

 @Test(expected=RuntimeException.class) public void testdecrpyMcpDataExceptions() throws Exception { Crypto mockCrypto = mock(Crypto.class); doThrow(new RuntimeException()).when(mockCrypto).closeSession(); mockCrypto.closeSession(); } 

works great.

+4
source

this line overwrites your layout:

 mockCrypto = CryptoManager.getCrypto(); 

and thatโ€™s why he fails.

+1
source

Source: https://habr.com/ru/post/1468993/


All Articles