Skip to content

Commit

Permalink
Capture testcase for unknown test failures in the JUnit Xml
Browse files Browse the repository at this point in the history
Testing Done:
Travis CI: https://travis-ci.org/pantsbuild/pants/builds/176145705

When the Mockito test runner comes across a mock that is unneeded it calls the test listener with a test failure for an unknown test case name.  We want to capture these failures as testcases in the xml output.

e.g.

```
Following stubbings are unnecessary (click to navigate to relevant line of code):
  1. -> at com.example.SampleTest.setUp(SampleTest.java:63)
Please remove unnecessary stubbings or use 'silent' option. More info: javadoc for UnnecessaryStubbingException class.
	at org.mockito.internal.exceptions.Reporter.formatUnncessaryStubbingException(Reporter.java:838)
	at org.mockito.internal.junit.UnnecessaryStubbingsReporter.validateUnusedStubs(UnnecessaryStubbingsReporter.java:34)
	at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:49)
	at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:104)
```

Bugs closed: 4054

Reviewed at https://rbcommons.com/s/twitter/r/4377/
  • Loading branch information
cheister committed Nov 15, 2016
1 parent 91b69b1 commit f4f732d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
9 changes: 9 additions & 0 deletions 3rdparty/jvm/org/mockito/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright 2016 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

jar_library(
name='mockito-core',
jars=[
jar(org='org.mockito', name='mockito-core', rev='2.2.16'),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,17 @@ public void testFailure(Failure failure) throws java.lang.Exception {
// problem with initialization and we won't get a testFinished callback so
// call finish here.
testCase.finished();
suite.testCases.clear();
suite.testCases.add(testCase);
suite.finished();
if (suite != null) {
suite.testCases.clear();
suite.testCases.add(testCase);
suite.finished();
}
} else {
testCase.finished();
testCase.setError(exception);
if (suite != null) {
suite.testCases.add(testCase);
}
}
} else {
if (isFailure) {
Expand Down
30 changes: 30 additions & 0 deletions tests/java/org/pantsbuild/tools/junit/impl/XmlReportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.pantsbuild.tools.junit.lib.XmlReportFailingTestRunnerTest;
import org.pantsbuild.tools.junit.lib.XmlReportFirstTestIngoredTest;
import org.pantsbuild.tools.junit.lib.XmlReportIgnoredTestSuiteTest;
import org.pantsbuild.tools.junit.lib.XmlReportMockitoStubbingTest;
import org.pantsbuild.tools.junit.lib.XmlReportTestSuite;

import static org.hamcrest.CoreMatchers.containsString;
Expand Down Expand Up @@ -122,6 +123,35 @@ public void testXmlReportAllIgnored() throws Exception {
assertEquals(testClassName, testSuite.getName());
}

@Test
public void testXmlReportMockitoUnnecessaryStubbing() throws Exception {
String testClassName = XmlReportMockitoStubbingTest.class.getCanonicalName();
AntJunitXmlReportListener.TestSuite testSuite = runTestAndParseXml(testClassName, true);

assertNotNull(testSuite);
assertEquals(1, testSuite.getTests());
assertEquals(0, testSuite.getFailures());
assertEquals(1, testSuite.getErrors());
assertEquals(0, testSuite.getSkipped());
assertTrue(Float.parseFloat(testSuite.getTime()) > 0);
assertEquals(testClassName, testSuite.getName());

List<AntJunitXmlReportListener.TestCase> testCases = testSuite.getTestCases();
assertEquals(2, testCases.size());
sortTestCasesByName(testCases);

AntJunitXmlReportListener.TestCase errorTestCase = testCases.get(1);
assertEquals(testClassName, errorTestCase.getClassname());
assertEquals("unnecessary Mockito stubbings", errorTestCase.getName());
assertEquals("0", errorTestCase.getTime());
assertNull(errorTestCase.getFailure());
assertThat(errorTestCase.getError().getMessage(),
containsString("Unnecessary stubbings detected in test class:"));
assertEquals("org.mockito.exceptions.misusing.UnnecessaryStubbingException",
errorTestCase.getError().getType());
assertThat(errorTestCase.getError().getStacktrace(), containsString(testClassName));
}

@Test
public void testXmlReportFirstTestIgnored() throws Exception {
String testClassName = XmlReportFirstTestIngoredTest.class.getCanonicalName();
Expand Down
1 change: 1 addition & 0 deletions tests/java/org/pantsbuild/tools/junit/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ java_library(
'3rdparty:guava',
'3rdparty:junit',
'3rdparty/jvm/org/hamcrest:hamcrest-core',
'3rdparty/jvm/org/mockito:mockito-core',
'src/java/org/pantsbuild/junit/annotations',
'src/java/org/pantsbuild/tools/junit',
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2016 Pants project contributors (see CONTRIBUTORS.md).
// Licensed under the Apache License, Version 2.0 (see LICENSE).

package org.pantsbuild.tools.junit.lib;

import javax.net.ssl.SSLSession;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Mockito.when;

/**
* This test is intentionally under a java_library() BUILD target so it will not be run
* on its own. It is run by the ConsoleRunnerTest suite to test ConsoleRunnerImpl.
*/
@RunWith(MockitoJUnitRunner.class)
public class XmlReportMockitoStubbingTest {
@Mock SSLSession sslSession;

@Test
public void testUnnecesaryMockingError() {
when(sslSession.getCipherSuite()).thenReturn("cipher");
Assert.assertTrue(true);
}
}

0 comments on commit f4f732d

Please sign in to comment.