Powermockito给了我一个空例外

时间:2014-08-08 17:21:23

标签: java

我正在尝试模拟一些静态方法,但我得到以下堆栈跟踪

java.lang.NullPointerException
at de.unibonn.iai.eis.luzzu.semantics.utilities.SPARQLHelper.toSPARQL(SPARQLHelper.java:12)
at de.unibonn.iai.eis.luzzu.semantics.utilities.DAQHelper.getDomainResource(DAQHelper.java:51)
at de.unibonn.iai.eis.luzzu.semantics.utilities.DAQHelper.getCategoryResource(DAQHelper.java:47)
at de.unibonn.iai.eis.luzzu.annotations.QualityMetadataTest.createMockedMetric(QualityMetadataTest.java:211)
at de.unibonn.iai.eis.luzzu.annotations.QualityMetadataTest.<init>(QualityMetadataTest.java:81)
....

DAQHelper是一个静态类,它是从我正在测试的方法调用的。为了简化故事,我有一个方法addMetricData(...),它调用getCategoryResource(...)等。这个getCategoryResource(..)是一个公共静态方法,它反过来调用getDomainResource(DAQHelper中的私有静态方法) )。 getDomainResource(...)方法调用静态SPARQLHelper.toSPARQL(...)方法。

你知道我为什么会遇到这个问题吗?测试代码如下:

@RunWith(Parameterized.class)

@PrepareForTest({DAQHelper.class,SPARQLHelper.class}) 公共类QualityMetadataTest扩展了Assert {

@Rule
public PowerMockRule rule = new PowerMockRule();

private QualityMetric metric;
private Model m = ModelFactory.createDefaultModel();
private QualityMetadata _testClass;
private Dataset _dataset;

private Resource metricResource = m.createResource("example:mockedMetric");
private Resource categoryResource = m.createResource("example:mockedCategory");
private Resource dimensionResource = m.createResource("example:mockedDimension");
private Resource dimensionProperty = m.createResource("example:hasMockedDimensionProperty");
private Resource metricProperty = m.createResource("example:hasMockedMetricProperty");
private Resource computedOn = m.createResource("example:testing");
private Resource qualityGraphURI;

private Resource categoryURI;
private Resource dimensionURI;
private Resource metricURI;

private boolean parameter;

@Parameters
public static Collection<Object[]> data() {
    Object[][] data = new Object[][] { { false } };
    return Arrays.asList(data);
}

public QualityMetadataTest(boolean QualityMetadataExists) throws Exception{
    this.parameter = QualityMetadataExists;
    if (QualityMetadataExists){
        // fill model m with quality metadata
    }

    this.createMockedMetric();

    _testClass = new QualityMetadata(m,computedOn);
    _testClass.addMetricData(metric);
    _dataset = _testClass.createQualityMetadata();
} 
// some test methods
    private void createMockedMetric() throws Exception{     
    metric = mock(QualityMetric.class);
    when(metric.getMetricURI()).thenReturn(metricResource);
    when(metric.metricValue()).thenReturn(0.5);


    mockStatic(DAQHelper.class);    
    PowerMockito.when(DAQHelper.getCategoryResource(metricResource)).thenReturn(categoryResource);
    PowerMockito.when(DAQHelper.getDimensionResource(metricResource)).thenReturn(dimensionResource);
    PowerMockito.when(DAQHelper.getPropertyResource(dimensionResource)).thenReturn(dimensionProperty);
    PowerMockito.when(DAQHelper.getPropertyResource(metricResource)).thenReturn(metricProperty);
}

我的信念是,一旦“何时”创建了方法,它们就会全局创建,并不会真正执行该方法,只会返回结果。我可能会有误解。你有什么想法吗?

干杯!

1 个答案:

答案 0 :(得分:0)

来自javadocAll usages require @RunWith(PowerMockRunner.class) and @PrepareForTest annotated at class level.因此,@PrepareForTest@Parameterized

一起使用似乎不合适