我正在尝试编写一个 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)