简体   繁体   English

使用 mockito 的 null 指针出现问题

[英]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.并且在这个方法中,实例化了一个新的Object,但是我不要它,否则会测试其他的class。 How I tell to mockito dont intanciate it?我如何告诉 mockito 不要煽动它?

@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:而我的测试 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.我不希望 mockito 实例化一个新的 DocuSign,因为这不是测试的原因。 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.好吧,Mockito 如果您的代码(要测试的代码,您打算执行的操作)执行某些操作,则无法更改某些内容,但是您可以对其进行模拟,使其不会创建新的 object (而是拥有您的“模拟对象”),以便您可以根据预期行为验证某些内容。

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.在您的代码中,如果您更改几行,您可以实现您想要的,例如 - 创建一个 DocuSignService class 并在那里创建这个新的 object 说一些 - getDocuSign 方法。 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.现在您可以控制这个 object。

I resolved it using powerMockito.

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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