我正在尝试编写一个java程序,允许用户输入一系列数据值然后程序通过查找该系列的均值和中位数进行排序,使用接口将“处理”不同的对象如果它们是相同的。但是,当我运行我的JUnit测试时,它一直说我的接口为空。
NoiseFilter.java
package noisefilter.model;
import java.util.ArrayList;
public interface NoiseFilter {
public double getBestMesurement(ArrayList<Double> samples);
}
AveragingFilter.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());
}
}
SensorData.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 Test)
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)
答案 0 :(得分: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;
}
当你拨打上述内容时, filterVar
为null
。
您必须使用界面实现对其进行初始化,例如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)
上调用x
方法。在Java中,null
没有任何方法。您始终必须使用==
运算符检查其标识的空值。您的示例应为:
if( x == null ) { ...
答案 1 :(得分:1)
您的setUp()方法不会创建过滤器。
它创建一个SensorData实例,然后将null值传递给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);