我有我的JUnit测试类:春季4 - 对象不会自动装配可变
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { AppConfig.class })
@WebAppConfiguration
public class TelephoneNumberTest {
@Autowired //Test that bean exists in application context
private void setPhoneNumberValidator(PhoneNumberValidator phoneNumberValidator){
this.phoneNumberValidator = phoneNumberValidator;
}
@Test
public void autoWireValidatorTest(){
assertNotNull(phoneNumberValidator); //Test passes
}
@Test
public void validUSNumber(){
PhoneNumber phoneNumber = null;
ContactType expectedContactType = ContactType.PRIMARY;
Country expectedCountry = Country.UNITED_STATES;
String expectedAreaCode = "940";
String expectedExchangeCode = "368";
String expectedLocalNumber = "7410";
try {
phoneNumber = new TelephoneNumber(expectedContactType, expectedCountry, expectedAreaCode,
expectedExchangeCode, expectedLocalNumber);
} catch (InvalidPhoneNumberException e) {
fail(e.getMessage());
}
assertNotNull(phoneNumber);
assertThat(phoneNumber.getContactType(), is(expectedContactType));
assertThat(phoneNumber.getCountry(), is(expectedCountry));
assertThat(phoneNumber.getAreaCode(), is(expectedAreaCode));
assertThat(phoneNumber.getExchangeCode(), is(expectedExchangeCode));
assertThat(phoneNumber.getLocalNumber(), is(expectedLocalNumber));
}
}
我的AppConfig
@Configuration
@EnableWebMvc
@ComponentScan("software.userprofile")
public class AppConfig {}
我PhoneNumber对象
package software.userprofile.models;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import software.userprofile.models.exceptions.InvalidPhoneNumberException;
import software.userprofile.models.validators.*;
import java.util.Comparator;
public class TelephoneNumber implements PhoneNumber{
private ContactType contactType;
private Country country;
private String areaCode;
private String exchangeCode;
private String localNumber;
private PhoneNumberValidator phoneNumberValidator;
public TelephoneNumber() {}
public TelephoneNumber(ContactType contactType, Country country, String areaCode, String exchangeCode, String localNumber) throws InvalidPhoneNumberException {
this.contactType = contactType;
this.country = country;
this.areaCode = areaCode;
this.exchangeCode = exchangeCode;
this.localNumber = localNumber;
validate();
}
public ContactType getType() {
return contactType;
}
public Country getCountry() {
return country;
}
public String getAreaCode() {
return areaCode;
}
public String getExchangeCode(){return exchangeCode;}
public String getLocalNumber() {
return localNumber;
}
public PhoneNumberValidator getPhoneNumberValidator(){
return phoneNumberValidator;
}
@Autowired
private void setPhoneNumberValidator(PhoneNumberValidator phoneNumberValidator){
this.phoneNumberValidator = phoneNumberValidator;
}
public void validate() throws InvalidPhoneNumberException {
phoneNumberValidator.doValidation(country, this);
}
@Override
public String toString() {
return "TelephoneNumber{" +
"contactType=" + contactType +
", country='" + country + '\'' +
", areaCode='" + areaCode + '\'' +
", exchangeCode='" + exchangeCode + '\'' +
", localNumber='" + localNumber + '\'' +
'}';
}
public static Comparator<TelephoneNumber> COMPARE_BY_TYPE = new Comparator<TelephoneNumber>() {
public int compare(TelephoneNumber one, TelephoneNumber other) {
return one.getType().compareTo(other.getType());
}
};
}
,最后我PhoneNumberValidator类用于
package software.userprofile.models.validators;
import org.springframework.stereotype.Component;
import software.userprofile.models.Country;
import software.userprofile.models.PhoneNumber;
import software.userprofile.models.exceptions.InvalidPhoneNumberException;
import java.util.*;
@Component
public class PhoneNumberValidator {
private Map<Country, PhoneNumberCountryValidator> phoneNumberValidators = new HashMap<Country, PhoneNumberCountryValidator>();
private Country defaultCountry;
public PhoneNumberValidator(){
init();
}
private void init(){
phoneNumberValidators.put(Country.UNITED_STATES, new USPhoneNumberCountryValidator());
phoneNumberValidators.put(Country.UNITED_KINGDOM, new UKPhoneNumberValidator());
phoneNumberValidators.put(Country.MEXICO, new MexicoPhoneNumberCountryValidator());
phoneNumberValidators.put(Country.CANADA, new CanadaPhoneNumberValidator());
}
public void doValidation(Country country, PhoneNumber phoneNumber) throws InvalidPhoneNumberException {
if(country == null){throw new InvalidPhoneNumberException("Country code is null");}
phoneNumberValidators.get(country).validate(phoneNumber);
}
public void setPhoneNumberValidators(Map<Country, PhoneNumberCountryValidator> phoneNumberValidators) {
this.phoneNumberValidators = phoneNumberValidators;
}
}
我的问题:在TelephoneNumberClass的PhoneNumberValidator对象不自动装配,当我运行测试,我在PhoneNumberValidator的doValidation()实例得到一个空指针异常。我能想到的唯一情况是TelephoneNumber类在春天不被识别,但我不知道如何解决这个问题。
我看过网站上的几篇文章,但没有任何工作。
任何想法?
我只是试过,但没有运气:(我得到了相同的错误 – jaredramirez
在你的测试类中,你不是注入spring bean而是像这样手动创建一个'PhoneNumber'的实例'phoneNumber = new TelephoneNumber',因此只需调用'phoneNumber.setPhoneNumberValidator'或注入'phoneNumber' bean即可 – sol4me