简体   繁体   中英

Problems with null pointer using mockito

I'm trying to test a method. And in this method, a new Object is instancied, but I don't want it, otherwise other class will be tested. How I tell to mockito dont intanciate it?

@Component
@EnableScheduling
public class VerificadorDeNovasAssinaturas { 

    private DocuSign docuSign;
    private ApiClient apiClient;
    @Autowired
    private DocuSignProperties docuSignProperties;

    public EnvelopesInformation verificaNovasAssinaturas() throws Exception {
        this.docuSign = new DocuSign(docuSignProperties); // I don't want mockito instanciate DocuSign
        this.apiClient = docuSign.getApiClient();
        this.apiClient.addDefaultHeader("Authorization", "Bearer " + docuSign.getoAuthToken().getAccessToken());

And my test class:

@SpringBootTest
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
public class VerificadorDeNovasAssinaturasTest {

@InjectMocks
private VerificadorDeNovasAssinaturas verificador;

private DocuSignProperties docuSignProperties;
private ApiClient apiClient;
private UserInfo userInfo; 
private OAuthToken oAuthToken;

@Mock
private DocuSign docuSign;


@Before
public void initialize() throws Exception {
    docuSignProperties = new DocuSignProperties();
    docuSignProperties.setBaseUrl("https://demo.docusign.net/restapi");
    docuSignProperties.setBasePath("/restapi");
    setApiClientConfigurations();
    when(docuSign.getApiClient()).thenReturn(this.apiClient);        
    when(docuSign.getoAuthToken()).thenReturn(this.oAuthToken);
    ...}

private void setApiClientConfigurations() throws Exception {
    this.apiClient = new ApiClient(this.docuSignProperties.getBaseUrl());
    this.oAuthToken = getOAuth();
            ... }
 @Test
 public void testaVerificacaoDeNovasAssinaturas() throws Exception {
    EnvelopesInformation results = verificador.verificaNovasAssinaturas();
    assertNotNull(results);
}

I don't want mockito instanciate a new DocuSign, because this is not the reason of the test. There is some way do ignore this step?

Well, Mockito can not change something if your code( Code to be tested, Which you intend to) does something, However you can mock it so that it does not create a new object (rather have your "Mocked Object"), so that you can verify something against the expected behavior.

In your code if you change few lines, you can achieve what you want, like - Create a DocuSignService class and there you create this new object in say some - getDocuSign method. Then your code looks something like below -

@Autowired
private DocuSignService docuSignService ;

this.docuSign = new DocuSign(docuSignProperties); // This is what you have
this.docuSign = this.docuSignService.getDocuSign() ; // This is new code

Now in your test case -

@Mock
DocuSignService docuSignService ;
@Mock
private DocuSign docuSign;
//.
//.
Mockito.when(this.docuSignService.getDocuSign()).thenReturn(docuSign);

Now you have control on this object.

I resolved it using powerMockito.

DocuSign docuSign = PowerMockito.mock(DocuSign.class);
PowerMockito.whenNew(DocuSign.class).withAnyArguments().thenReturn(docuSign);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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