簡體   English   中英

Junit測試類失敗

[英]Junit Test Class Failure

我為以下方法編寫了Junit測試:

@Service
public class EnterpriseCustomerVerificationService {

 @Autowired
 CreditCheckRepository creditCheckRepository;

 public String FTACheck(StandardInputHeader charterHeaderInputInfo,
            Holder<StandardOutputHeader> charterHeaderOutputInfo, Long sys, Long prin, Long agent)
            throws EnterpriseCustomerVerificationException {

        String prepay = null;
        List<String> prepays = null;

        charterHeaderOutputInfo.value = new StandardOutputHeader(charterHeaderInputInfo);
        prepays = creditCheckRepository.getCSGPrepay(sys.toString(), prin.toString(), agent.toString(),
                charterHeaderInputInfo.getAuditUser());

        if (prepays != null && prepays.size() > 0) {
            prepay = prepays.get(0);
        }

        return prepay;
    }
}

CreditCheckRepository類

@Component
public class CreditCheckRepository {

    @Autowired
    SPGetCSGPrepay spGetCSGPrepay;

    public List<String> getCSGPrepay(String sys, String prin, String agent, String auditUser) {
        return spGetCSGPrepay.execute(sys, prin, agent, auditUser);
    }
}

測試方法

package com.charter.enterprise.credit.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.*;

import javax.xml.ws.Holder;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.test.util.ReflectionTestUtils;

@RunWith(MockitoJUnitRunner.class)
public class EnterpriseCustomerVerificationServiceTest {

    @InjectMocks
    EnterpriseCustomerVerificationService 
    enterpriseCustormerVerificationServiceMock;


    @Mock
    CreditCheckRepository creditCheckRepository;

    @Rule
    public ExpectedException thrown = ExpectedException.none();
    StandardInputHeader charterHeaderInputInfo;
    Holder<StandardOutputHeader> charterHeaderOutputInfo;

    @Before
    public void setup() {

        charterHeaderInputInfo = new StandardInputHeader();
        charterHeaderInputInfo.setAuditUser("test");
        charterHeaderInputInfo.setClientIp("127.0.0.1");
        charterHeaderOutputInfo = new Holder<StandardOutputHeader>();


        ReflectionTestUtils.setField(
                enterpriseCustormerVerificationServiceMock,
                "creditCheckRepository", creditCheckRepository);

    }
    @Test
        public void getFTACheckTest() {

            String auditUser = "User";
            Long sys = 123L;
            Long prin = 456L;
            Long agent = 789L;

            List<String> prepays = new ArrayList<String>();
            prepays.add("ABCDEF");

            Mockito.when(
                    creditCheckRepository.getCSGPrepay("123", "456", "789", "User"))
                    .thenReturn(prepays);
            // Mockito.when(spGetCSGPrepay.execute("123", "456","789",
            // "User")).thenReturn(prepays);
            String prepay = enterpriseCustormerVerificationServiceMock.FTACheck(
                    charterHeaderInputInfo, charterHeaderOutputInfo, sys, prin,
                    agent);
            assertNotNull(prepay);
    }
}

我的問題是,每當嘗試對prepay Object進行斷言檢查時,我總會為null。 我也將Mocked標記為EnterpriseCustomerVerificationService enterpriseCustormerVerificationServiceMock; 請指出我的業務邏輯有問題還是什么?

您對自己的論點做了些大錯:

creditCheckRepository.getCSGPrepay("123", "456", "789", "User"))

應該

creditCheckRepository.getCSGPrepay("123", "456", "789", "test"))

作為附帶說明,您應該使用ArgumentMatchers (如果使用舊版本,則可能是Matchers ),以避免出現此類問題:

Mockito.when(creditCheckRepository.getCSGPrepay(ArgumentMatchers.anyString(), ArgumentMatchers.anyString(),
   ArgumentMatchers.anyString(), ArgumentMatchers.anyString())).thenReturn(prepays);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM