0

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?

4
  • 3
    Pure mockito can not help you here. You could use powermockito's whenNew functionality to replace it with a mock. Otherwise you will have to refactor your code to provide DocuSign as a dependency instead. Commented Oct 3, 2019 at 15:32
  • maybe something like this if you want to call real methods from mock object other than the mocked methods. Stock MOCK_STOCK = Mockito.mock( Stock.class, CALLS_REAL_METHODS ); Commented Oct 3, 2019 at 15:40
  • With mockito, the only thing that you can do is have the new DocuSign(docuSignProperties); returned by a separate method, which you can mock. But anyways, I'd like to know where you're getting a null pointer as is right now. Commented Oct 3, 2019 at 15:43
  • You can use mockito to mock the stubbed object's method Commented Oct 3, 2019 at 16:41

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0
I resolved it using powerMockito.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.