2017-09-04 112 views
0

我已经写了JUnit测试: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为EnterpriseCustomerVerificationService enterpriseCustormerVerificationServiceMock; 请问我的商业逻辑有问题吗?

+0

你有没有调试过它,并检查你是否嘲笑正确的值? – Stultuske

+0

是的,我已经调试过它。但对象预付款在每次显示为空时都没有获得任何价值。 –

+1

Mockito.when( creditCheckRepository.getCSGPrepay(“123”,“456”,“789”,“User”)) .thenReturn(prepays);为什么你硬编码的价值,使用匹配器,而不是像anyString,任何() –

回答

1

你做了一些糟糕!你的论点:

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); 
+1

为什么'anyString()' - 在2017年只是'any()'?! – GhostCat

+0

@RC由于其现在的工作,如果我用匹配器是这样的:Mockito.when( \t \t \t \t creditCheckRepository.getCSGPrepay(Matchers.anyString(),Matchers.anyString(),Matchers.anyString(),Matchers.anyString() )) \t \t \t \t .thenReturn(prepays); –