简体   繁体   中英

Problems with dependency injection using Mockito

I'm trying to inject a dependency into my class. But for some reason when I am testing it using Mockito, it doesn't work and the object isn't instantiated and remains null.

@Data
@Component
@ConfigurationProperties(prefix="docusign")
public class DocuSignProperties {

private String oAuthBaseUrl;
private String baseUrl;
}

In application.properties:

#docuSign properties
docusign.oAuthBaseUrl=https://account-d.docusign.com/oauth
docusign.baseUrl=https://demo.docusign.net/restapi

The class that is injecting DocuSignProperties:

@Component
public class DocuSign {
    @Autowired
    private DocuSignProperties docuSignProperties;

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

    public DocuSign() {
        this.initialize();
}

    public DocuSign(ProjetoDTO projeto)  {
        this.projeto = projeto;
        this.initialize();
}

    private void initialize() {
    try {
        this.apiClient = new ApiClient(this.docuSignProperties.getBaseUrl()); //here docuSignProperties is null, when should it be instantiated
        this.oAuthToken = getOAuthTokenAcesso();
        this.setApiClientConfigurations();
    }catch (Exception e) {
        LOGGER.error(e.getMessage());
        e.printStackTrace();
    }
}

The class that tests it:

public class DocuSignTest {
    private DocuSignProperties docuSignProperties;
    private ProjetoDTO projeto1;
    @Mock
    private DocuSignProperties docuSignProperties;

    @Before
    public void setup() throws IOException {
       projeto1 = new ProjetoDTO();
       projeto1.setId(1L);
       projeto1.setDsProjeto("projeto1")
    }
  @Test
  public void criarDocumento() throws Exception {
      DocuSign docuSign = new DocuSign(projeto1);
      docuSign.sendDoc();
      assertNotNull(docuSign);
}

I would like to know why on initialize method docuSignProperties isn't instantiated.

to use annotation Mock you habe to use:

 MockitoAnnotations.initMocks(this);

or add at the beginning of the clas:

@RunWith(org.mockito.runners.MockitoJUnitRunner.class)

moreover you have to inject your mock you can use @InjectMocks what I would rather do not recommend but change your constructor or use a setter it depends if that class is mandatory or optional:

 @Autowired
 public DocuSign(DocuSignProperties docuSignProperties) {
    this.initialize();
    this.docuSignProperties=docuSignProperties;
 }

then in your test when creating your DocuSign you just pass prodSignProj (your mock) as a parameter

I change the contructor to

public DocuSign(DocuSignProperties docuSignProperties) {
    this.initialize(docuSignProperties);
}

public DocuSign(ProjetoDTO projeto, DocuSignProperties docuSignProperties)  {
    this.projeto = projeto;
    this.initialize(docuSignProperties);
}

private void initialize(DocuSignProperties docuSignProperties) {...}

and on my test class:

@Before
public void setup() throws IOException {
   docuSignProperties = new DocuSignProperties();
   docuSignProperties.setBaseUrl("https://demo.docusign.net/restapi");
   docuSignProperties.setBasePath("/restapi");

@Test
public void criarDocumento() throws Exception {
    DocuSign docuSign = new DocuSign (projeto1, docuSignProperties);
    docuSign.sendDoc();
    assertNotNull(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