jmockit / jmockit1

Advanced Java library for integration testing, mocking, faking, and code coverage
Other
465 stars 240 forks source link

Coverage Report Not generating due to mocking jre classes #166

Closed manusaxena closed 9 years ago

manusaxena commented 9 years ago

Hello,

I am using jmockit latest release in project and coverage report is getting failed due mock url class. i read the release history and saying that issue is already resolved but i am getting here. I am placing ant error log here. any sort of help would be appreciated.

[junit] Exception in thread "Thread-0" java.lang.NullPointerException [junit] at java.io.File.(File.java:251) [junit] at mockit.coverage.modification.ClassesNotLoaded.gatherCoverageData(ClassesNotLoaded.java:28) [junit] at mockit.coverage.OutputFileGenerator.generate(OutputFileGenerator.java:82) [junit] at mockit.coverage.CodeCoverage$1.run(CodeCoverage.java:67)

Here is the test class for reference.

@RunWith(JMockit.class) public class GetImagesServiceTest extends BaseUnitTestCase {

/**
 * GetImagesService imageService
 */
@Tested
protected GetImagesService target = new GetImagesService();

/**
 * HttpURLConnection connection
 */
@Mocked
private HttpURLConnection connection;

/**
 * HttpGet httpGet
 */
@Mocked
private HttpGet httpGet;

/**
 * URL url
 */
@Mocked
private URL url;
/**
 * HydraContentRepository holding hydraContent repository.
 */
@Mocked
private HydraContentRepository hydraContentRepository;

/**
 * HydraHtmlContentRepository holding htmlContent repository.
 */
@Mocked
private HydraHtmlContentRepository hydraHtmlContentRepository;

/**
 * pre process setup before executing any test case.
 */
@Before
public void setup() {
    Deencapsulation.setField(target, hydraContentRepository);
    Deencapsulation.setField(target, hydraHtmlContentRepository);

}

*/ @Test public void testRejectDomain() throws MalformedURLException { String url; URL urlOb; url = "http://www.googlesyndication.com/test/apc"; urlOb = new URL(url); mockHost(urlOb, "www.googlesyndication.com"); Assert.assertFalse(GetImagesService.checkImage(urlOb)); url = "http://www.doubleclick.net/test/apc"; urlOb = new URL(url); mockHost(urlOb, "www.doubleclick.net"); Assert.assertFalse(GetImagesService.checkImage(urlOb)); url = "http://www.scorecardresearch.com/test/apc"; urlOb = new URL(url); mockHost(urlOb, "www.scorecardresearch.com"); Assert.assertFalse(GetImagesService.checkImage(urlOb)); url = "http://www35.glam.com/test/apc"; urlOb = new URL(url); mockHost(urlOb, "www35.glam.com"); Assert.assertFalse(GetImagesService.checkImage(urlOb));

}

*/
private void mockHost(final URL url, final String host) {

    new Expectations() {
        {
            url.getHost();
            result = host;
        }
    };

}

/**
 * mocking file attribute of URL.
 *
 * @param url
 *            URL
 * @param file
 *            String
 */
private void mockFile(final URL url, final String file) {

    new Expectations() {
        {
            url.getFile();
            result = file;
        }
    };

}

}

ANT call for test

rliesenfeld commented 9 years ago

I executed a JUnit test suite containing @Mocked fields for URL and HttpURLConnection, from IntelliJ IDEA, Maven, and Ant. But there was no NullPointerException at the end, and the coverage report got correctly generated in each case.