Junit测试用例初始化一个出现错误的类

时间:2018-07-16 16:59:46

标签: java class testing junit

我正在为StandardAlone Java类编写Junit测试用例。初始化Class对象时,我得到nullPointerException。这是我的代码。任何帮助表示赞赏

这是我的课程

@RunWith(MockitoJUnitRunner.class)
public class ABCGeneratorApplicationTest extends TestCase {

   // @Autowired
    MyFileReader myFileReader;

public ABCGeneratorApplicationTest (MyFileReader myFileReader) {

    this.myFileReader= myFileReader;
}

@Before
public void setUp() throws Exception {


}

@Test
public void instrumentRead()
{
    String[] args = {"20180518","0002"};
    Stream<Employee> empList= myFileReader.read(args);
    Assert.assertNotNull(instrumentStream);
}

实施班

public class MyFileReader {

public Stream<Employee> read(String[] runArguments){

    try {
        Path path = getFilePath(runArguments);

        return Files.lines(path,getEncoding()).skip(1)
                .map(this::parseRow)
                .filter(this::filterRow);

    } catch (IOException e) {
        return Stream.empty();
    }catch(Exception e) {
        e.printStackTrace();
        return Stream.empty();
    }

}

这是我遇到的错误:

  

java.lang.Exception:测试类应该只有一个公共类   零参数构造函数

     在

  org.junit.runners.BlockJUnit4ClassRunner.validateZeroArgConstructor(BlockJUnit4ClassRunner.java:171)   在   org.junit.runners.BlockJUnit4ClassRunner.validateConstructor(BlockJUnit4ClassRunner.java:148)   在   org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:127)   在org.junit.runners.ParentRunner.validate(ParentRunner.java:416)处   org.junit.runners.ParentRunner。(ParentRunner.java:84)在   org.junit.runners.BlockJUnit4ClassRunner。(BlockJUnit4ClassRunner.java:65)   在   org.mockito.internal.runners.DefaultInternalRunner $ 1。(DefaultInternalRunner.java:27)   在   org.mockito.internal.runners.DefaultInternalRunner。(DefaultInternalRunner.java:27)   在sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native   方法)   sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)   在   sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)   在java.lang.reflect.Constructor.newInstance(Constructor.java:423)在   org.mockito.internal.runners.util.RunnerProvider.newInstance(RunnerProvider.java:27)   在   org.mockito.internal.runners.RunnerFactory.create(RunnerFactory.java:69)   在   org.mockito.internal.runners.RunnerFactory.createStrict(RunnerFactory.java:40)   在   org.mockito.junit.MockitoJUnitRunner。(MockitoJUnitRunner.java:152)   在sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native   方法)   sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)   在   sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)   在java.lang.reflect.Constructor.newInstance(Constructor.java:423)在   org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)   在   org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)   在   org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)   在   org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)   在   org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)   在   org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)   在   com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)   在   com.intellij.rt.execution.junit.IdeaTestRunner $ Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)   在   com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)   在   com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

1 个答案:

答案 0 :(得分:1)

这是因为您的测试类(在这种情况下为ABCGeneratorApplicationTest)没有任何默认的公共构造函数(因为您只有参数化的构造函数)。只需添加默认的公共构造函数,此异常即可解决。.