英文:
Trying to instantiate an Object in @Before
问题
我想测试 HerePoiSearchTransformers.class,但是我得到了以下错误:**java.lang.NullPointerException**
我有以下单元测试 **testTransformProviderResponse**:
public class HerePoiSearchTransformerTest {
private static final String ADDRESS_CATEGORIES = "city-town";
@Rule
public ErrorCollector collector = new ErrorCollector();
private final HerePoiSearchTransformer transformer = new HerePoiSearchTransformer();
// private final AddressDistrictCity addressDistrictCity = new AddressDistrictCity();
private AddressDistrictCity addressDistrictCity;
@Before
public void setUp() {
Whitebox.setInternalState(transformer, "hereAddressCategories", ADDRESS_CATEGORIES);
transformer.initHereCategoryClassification();
addressDistrictCity = new AddressDistrictCity();
}
@Test
public void testTransformProviderResponse() throws IOException, OnlinePOIsearchException {
final ProviderResponse hereResponse = new ProviderResponse();
hereResponse.setContent(content);
final List<PoiBE> pois = transformer.transformToPoiBEs(hereResponse);
final PoiBE poi = pois.get(0);
assertThat(poi.getDetails()).isNotNull();
}
}
!! **问题在于来自 AbstractHereTransformers 的 AddressDistrictCity 为 null** !!
我有以下结构:
class HerePoiSearchTransformers extends AbstractHereTransformer
方法:
transformToPoiBEs(...) {
transformItems(...);
}
transformItems(...) {
addAddress(final AddressDO resultAddress, final PoiBE poi)
}
class AbstractHereTransformers {
@Inject
private AddressDistrictCity addressDistrictCity;
protected void addAddress(final AddressDO resultAddress, final PoiBE poi) {
addressDistrictCity.setDistrictAndCity(address, resultAddress.getDistrict(), resultAddress.getCity());
}
}
我认为 `private final AddressDistrictCity addressDistrictCity = new AddressDistrictCity();` 会解决这个问题。
英文:
I want to test HerePoiSearchTransformers.class but I get the following error: java.lang.NullPointerException
I have the following unit test testTransformProviderResponse:
public class HerePoiSearchTransformerTest {
private static final String ADDRESS_CATEGORIES = "city-town";
@Rule
public ErrorCollector collector = new ErrorCollector();
private final HerePoiSearchTransformer transformer = new HerePoiSearchTransformer();
// private final AddressDistrictCity addressDistrictCity = new AddressDistrictCity();
private AddressDistrictCity addressDistrictCity;
@Before
public void setUp() {
Whitebox.setInternalState(transformer, "hereAddressCategories", ADDRESS_CATEGORIES);
transformer.initHereCategoryClassification();
addressDistrictCity = new AddressDistrictCity();
}
@Test
public void testTransformProviderResponse() throws IOException, OnlinePOIsearchException {
final ProviderResponse hereResponse = new ProviderResponse();
hereResponse.setContent(content);
final List<PoiBE> pois = transformer.transformToPoiBEs(hereResponse);
final PoiBE poi = pois.get(0);
assertThat(poi.getDetails()).isNotNull();
}
}
!! The problem is that AddressDistrictCity from AbstractHereTransformers is null !!
I have the following structure:
class HerePoiSearchTransformers extends AbstractHereTransformer
Methods:
transformToPoiBEs(...) {
transformItems(...);
}
transformItems(...) {
addAddress(final AddressDO resultAddress, final PoiBE poi)
}
class AbstractHereTransformers {
@Inject
private AddressDistrictCity addressDistrictCity;
protected void addAddress(final AddressDO resultAddress, final PoiBE poi) {
addressDistrictCity.setDistrictAndCity(address, resultAddress.getDistrict(), resultAddress.getCity());
}
}
I though that private final AddressDistrictCity addressDistrictCity = new AddressDistrictCity();
will fix the problem.
答案1
得分: 0
HerePoiSearchTransformer
内部的 AddressDistrictCity
未被初始化。
你创建了一个变量 addressDistrictCity = new AddressDistrictCity();
,但这不是 transformer
内部存在的对象。
你可以在 HerePoiSearchTransformer
上使用 @InjectMocks
,在 AddressDistrictCity
上使用 @Mock
。
@InjectMocks private HerePoiSearchTransformer transformer;
@Mock private AddressDistrictCity addressDistrictCity;
JUnit 框架将会确保在 AbstractHereTransformers
类中用所需的模拟对象替换 addressDistrictCity
。
英文:
The AddressDistrictCity
inside HerePoiSearchTransformer
is not initialized.
You created a variable addressDistrictCity = new AddressDistrictCity();
but this is not the object which is present inside transformer
.
You can use @InjectMocks
on HerePoiSearchTransformer
and @Mock
on AddressDistrictCity
@InjectMocks private HerePoiSearchTransformer transformer;
@Mock private AddressDistrictCity addressDistrictCity;
The JUnit framework will take care of replacing the addressDistrictCity with the required mock object in the AbstractHereTransformers
class.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论