1

我正在尝试编写一个 java 程序,该程序允许用户输入一系列数据值,然后程序通过查找该系列的平均值和中位数进行排序,使用接口“处理”不同的对象,就好像它们是相同。但是,当我运行 JUnit 测试时,它一直说我的接口为空。

噪声过滤器.java

package noisefilter.model;

import java.util.ArrayList;

public interface NoiseFilter {

public double getBestMesurement(ArrayList<Double> samples);

}

平均过滤器.java

package noisefilter.model;

import java.util.ArrayList;

public class AveragingFilter implements NoiseFilter {

@Override
/**
 * The method getBestMeasurement takes an array list of type double and returns a
 */
public double getBestMesurement(ArrayList<Double> samples) {
    // TODO Auto-generated method stub

    if (samples.equals(null)) {
        throw new IllegalArgumentException("The Samples cannot be null");
    }
    if (samples.size() < 3) {
        throw new IllegalArgumentException("The Sample size must have 3");
    }
    double sum = 0.0;
    for (Double number : samples) {
        sum += number;
    }

    return (sum / samples.size());
}

}

传感器数据.java

package noisefilter.model;

import java.util.ArrayList;

public class SensorData implements NoiseFilter {

ArrayList<Double> data;
NoiseFilter filter;

public SensorData() {
    this.data = new ArrayList<Double>();
    this.filter = null;
}

public void addSample(double sample) {
    data.add(sample);
}

public void setFilter(NoiseFilter filterVar) {
    this.filter = filterVar;
}

public double getFilteredResult() {
    if (filter.equals(null)) {
        throw new IllegalStateException("The filter cannot be left null");
    }
    return filter.getBestMesurement(data);
}

@Override
public double getBestMesurement(ArrayList<Double> samples) {

    samples = this.data;
    return 0;
}

WhenAveragingFilter.java (JUnit 测试)

package noisefilter.tests;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import noisefilter.model.NoiseFilter;
import noisefilter.model.SensorData;

public class WhenAverageFiltering {

NoiseFilter filter;
SensorData dataSet;


@Before
public void setUp() throws Exception {
    this.dataSet = new SensorData();
    dataSet.setFilter(filter);
}

@Test
public void averageOfThree100sShouldBe100() {
    dataSet.addSample(100);
    dataSet.addSample(100);
    dataSet.addSample(100);

    assertTrue(dataSet.getFilteredResult() == 100);
}

@Test
public void averageOfThreeDifferent() {
    dataSet.addSample(120);
    dataSet.addSample(140);
    dataSet.addSample(130);

    assertTrue(dataSet.getFilteredResult() == 130);
}

@Test
public void averageOfFiveDifferent() {
    dataSet.addSample(10);
    dataSet.addSample(50);
    dataSet.addSample(60);

    assertTrue(dataSet.getFilteredResult() == 40);
}

}

这是我在运行 JUnit 测试时遇到的错误。

java.lang.NullPointerException
at noisefilter.model.SensorData.getFilteredResult(SensorData.java:24)
at     noisefilter.tests.WhenAverageFiltering.averageOfFiveDifferent(WhenAverageFiltering.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
4

2 回答 2

4

filter您永远不会在测试类中初始化该字段

NoiseFilter filter;
SensorData dataSet;


@Before
public void setUp() throws Exception {
    this.dataSet = new SensorData();
    dataSet.setFilter(filter);
}

所以它就在null这里,并且该引用传播到SensorData您正在测试的对象。

public void setFilter(NoiseFilter filterVar) {
    this.filter = filterVar;
}

filterVarnull当你打电话给上面的。

您必须使用接口的实现来初始化它,例如AveragingFilter

@Before
public void setUp() throws Exception {
    this.filter = new AveragingFilter(); // or something like it
    this.dataSet = new SensorData();
    dataSet.setFilter(filter);
}

此外,您正在使用此模式在几个地方测试 null:

if( x.equals( null ) ) {...

这将不起作用,因为这是调用equals(Object)object 上的方法x。在Java中,null没有任何方法。您始终必须使用运算符检查 null 与它的identity==是否相符。您的示例应为:

if( x == null ) { ...
于 2013-09-12T18:07:38.673 回答
1

您的 setUp() 方法不会创建过滤器。

它创建一个 SensorData 实例,然后将一个空值传递给 setFilter。稍后调用 getFilteredResult() 时,它会遇到 NPE。

评论后补充:

匿名类:

filter = new NoiseFilter() {
    public double getBestMesurement(ArrayList<Double> samples) {
        return 100; // or do something else
    }
}

dataSet.setFilter(filter);

使用 Mockito:

import static org.mockito.Mockito.*;
import static org.mockito.Matchers.*

// ...

filter = Mockito.mock( NoiseFilter.class );
when( filter.getBestMesurement( anyListOf( Double.class ) ) ).thenReturn( 100.0 )

dataSet.setFilter(filter);
于 2013-09-12T18:09:42.817 回答