2021年1月25日星期一

Autowire inside of other class object getting null in Cucumber Test

I have this Step Definition which calls out CurrencyValidator. I wanted to create a unit test for it's isValid method

  public class ValidationStepDefs {            // Variables for the features Example      private String enumName;      private String ccy;            private CurrencyValidator currencyValidator = new ValidCurrencyValidator();        private String actualResult;            @Given("valid currency are provided {string}")      public void provideCcy(String ccy) {          this.ccy = ccy;      }            @When("the CurrencyValidator.isValid was called")      public void validCurrencyValidator() {            boolean isCcyValid = currencyValidator.isValid(this.ccy, null);                    if(isCcyValid) {              this.actualResult = "valid";          } else {              this.actualResult = "not valid";          }      }            @Then("the result should be {string}")      public void validation(String expectedResult) {          assertEquals(expectedResult, actualResult);      }             

This is the Currency Validator class. Which Autowired a service which gets all the currency from a database. But when I debug my code the service from CurrencyValidator class is getting a null as a value.

  public class CurrencyValidator implements ConstraintValidator<Currency, String> {        @Autowired      SampleService service;        @Override      public boolean isValid(String currency, ConstraintValidatorContext context) {          if (StringUtils.isNotBlank(currency)) {              return getAllCurrencies().contains(currency);          }            return true;      }          private List<String> getAllCurrencies() {          HashMap<String, Currency> CurrencyMap = refDataService.getKa4Currencies();          List<String> currencies = new ArrayList<String>(CurrencyMap.keySet());          return currencies;      }  }      
https://stackoverflow.com/questions/65895070/autowire-inside-of-other-class-object-getting-null-in-cucumber-test January 26, 2021 at 10:22AM

没有评论:

发表评论