单元测试无法正常工作-代替404,它返回200作为状态代码

我正在为应用程序编写单元测试,该应用程序将特定货币的金额转换为EUR。我在数据库中有一些帐户,这些帐户有:iban,货币和余额。 为了获取所有货币及其在欧元中的价值,需要进行API调用以获取汇率。转换方法接收帐户的iban并以EUR的形式返回该帐户的金额。 该应用程序运行正常,但是单元测试存在一些问题。

我正在尝试为无法找到帐户的情况编写一个单元测试-因此,当“ AccountController”类中的方法-“ getAccountByIban”返回404时。我编写了测试并将IBAN添加为随机字符串-但不是404 ,我得到200作为响应代码,测试失败。如果我从测试中使用该随机IBAN调用方法“ getAccountByIban”,我将获得正确的响应代码-404。

这是我的代码:

AccountController:

@RestController
public class AccountController {

    @Autowired
    RequestBuilder requestBuilder;

    @Autowired
    AccountService accountService;

    @ApiOperation(value = "Get account with given iban ", response = AccountEntity.class)
    @RequestMapping(value = "/account/{iban}", method = RequestMethod.GET )
    public AccountDTO getAccountByIban(@PathVariable("iban") String iban ) throws AccountNotFoundException {

        return Optional
                .ofNullable(accountService.getAccountByIban(iban))
                .orElseThrow(() -> new AccountNotFoundException("Requested account was not found! [ iban = " + iban + "]"));
    }

    @ApiOperation(value = "Get account with amount converted ", response = AccountEntity.class)
    @RequestMapping(value = "/account/exchange/{iban}", method = RequestMethod.GET )
    public AccountDTO getAmountConvertedByIban(@PathVariable("iban") String iban) {

        AccountDTO accountDTO = accountService.getAccountByIban(iban);
        Double currentBalance = accountDTO.getBalance();
        String currentCurrency = accountDTO.getCurrency();
        Double actualBalance = accountService.getCurrencyValue(currentCurrency) * currentBalance;
        accountDTO.setBalance(actualBalance);
        return accountDTO;
    }
} 

AccountService:

public class AccountService {

    @Autowired
    AccountRepository accountRepository;

    @Autowired
    RequestBuilder requestBuilder;

    @Autowired
    CurrencyDTO currencyDTO;

    @Cacheable("exchangeRates")
    public Double getCurrencyValue(String currency) {

        return currencyDTO.getCurrencyValue(currency);

    }

    public AccountDTO getAccountByIban(String iban){

        AccountDTO accountDTO = null;
        AccountEntity accountEntity = accountRepository.findByIban(iban);

        if (accountEntity != null ) {
            accountDTO = accountEntity.toDTO();
        }

        return accountDTO;
    }
}

带有UNIT测试的课程:

@AutoConfigureMockMvc
@RunWith(SpringRunner.class)
@SpringBootTest
public class AccountServiceTest {


    @MockBean
    AccountController accountController;

    @Spy
    @InjectMocks
    AccountService accountService;

    @Mock
    AccountRepository accountRepository;

    @Autowired
    MockMvc mockMvc;

    @Before
    public void setup() throws Exception {
        AccountEntity accountEntity = new AccountEntity();
        accountEntity.setIban("RO06PORL4513558777471397");
        accountEntity.setBalance(10000D);
        accountEntity.setCurrency("RON");

        Mockito.when(accountRepository.save(accountEntity)).thenReturn(accountEntity);
    }

    @Test
    public void testAccountNotFound() throws Exception {

        mockMvc.perform(get("/account/RO2E2"))
                .andExpect(status().isNotFound());
        //Assert.assertTrue(accountService.getAccountByIban("RO06PORL4513558777471397") == null);

帐户DTO:

public class AccountDTO {

    @ApiModelProperty(notes = "Account ID",name="id",required=true,value="id")
    private int id;

    @ApiModelProperty(notes = "Account IBAN",name="iban",required=true,value="iban")
    private String iban;

    @ApiModelProperty(notes = "Account Currency",name="currency",required=true,value="currency")
    private String currency;

    @ApiModelProperty(notes = "Account Balance",name="balance",required=true,value="balance")
    private Double balance;

    @ApiModelProperty(notes = "Last Update",name="lastUpdate",required=true,value="lastUpdate")
    private Date lastUpdate;

谁能帮助我改正我的考试课程?我看不到我在做什么错

评论