尝试在@Before中实例化一个对象。

huangapple go评论148阅读模式
英文:

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 = &quot;city-town&quot;;
@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, &quot;hereAddressCategories&quot;, ADDRESS_CATEGORIES);
transformer.initHereCategoryClassification();
addressDistrictCity = new AddressDistrictCity();
}
@Test
public void testTransformProviderResponse() throws IOException, OnlinePOIsearchException {
final ProviderResponse hereResponse = new ProviderResponse();
hereResponse.setContent(content);
final List&lt;PoiBE&gt; 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.

huangapple
  • 本文由 发表于 2020年10月13日 23:07:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64337901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定