gpt4 book ai didi

java - Junit 测试类失败

转载 作者:行者123 更新时间:2023-12-02 12:17:45 25 4
gpt4 key购买 nike

我已经为以下方法编写了 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);
}
}

我的问题是,每当我尝试对预付对象进行断言检查时,我总是得到空值。我还将Mocked注入(inject)为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);

关于java - Junit 测试类失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46035295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com