mardi 25 août 2015

initializationError while mocking Singleton - Test class should have exactly one public constructor/No runnable methods

I am using Mockito API, TestNG and RunListener to unit test a Singleton and log results.

Here is the original class to be mocked:

import java.io.InputStream;

/**
* The class FileLoader.
*
* @author Sandeep Chatterjee
* @since 25/8/2015
*/
public class FileLoader implements Loadable {

    /**
    * The Constant FILE_LOADER_INSTANCE.
    */
    private static final FileLoader FILE_LOADER_INSTANCE = new FileLoader();


    /**
    * Instantiates a new file loader.
    */
    private FileLoader() {
        if (FILE_LOADER_INSTANCE != null) {
            throw new IllegalStateException("FileLoader already instantiated!");
        }
    }

    /**
    * @return A single instance of FileLoader
    */
    public static FileLoader getInstance() {
        return FILE_LOADER_INSTANCE;
    }

    /**
    * @param resourcePath The path to input resource
    * @return An input stream
    */
    @Override
    public InputStream loadResource(String resourcePath) {
        return FileLoader.class.getResourceAsStream(resourcePath);
    }
}

Code to run the tests:

JUnitCore runner = new JUnitCore();
ExecutionListener executionListener = new ExecutionListener();
runner.addListener(executionListener);
runner.run(FileLoader.class);

But the test cases fail:

initializationError(com.github.sndpchatterjee07.service.FileLoader). Test class should have exactly one public constructor
initializationError(com.github.sndpchatterjee07.service.FileLoader). No runnable methods

Questions:

  • How do I fix this?
  • Is there a better way out to test the Singleton in this case?

Aucun commentaire:

Enregistrer un commentaire