java - Mock final new instance created? -


how mock private final mockclass mockclass = new mockclass();

 public class simpleclass {      private final mockclass mockclass = new mockclass();  } 

use dependency injection; can have whatever want in field:

class simpleclass {   private final mockclass mockclass;    simpleclass(mockclass mockclass) {     this.mockclass = mockclass;   } } 

see misko hevery's guide writing testable code: code example of warning sign "new keyword in constructor or @ field declaration".


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -