Skip to content

Commit

Permalink
Exclude org.hamcrest from the shading rules for junit runner
Browse files Browse the repository at this point in the history
The shading of the junit runner specifically excludes the org.junit classes.  As it turns out, we also need to exempt the org.hamcrest classes so that APIs work correctly.

Testing Done:
CI is running at: https://travis-ci.org/pantsbuild/pants/builds/60578769

We use 'assertThat' a fair amount in our code base.  This change fixes those problems.

Bugs closed: 1466, 1467

Reviewed at https://rbcommons.com/s/twitter/r/2137/
  • Loading branch information
ericzundel committed Apr 29, 2015
1 parent 17c3925 commit 35a513b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/python/pants/backend/jvm/tasks/junit_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ def register_options(cls, register, register_jvm_tool):
# Clearly both tests and the runner need access to the same @Test, @Before,
# as well as other annotations, but there is also the Assert class and some
# subset of the @Rules, @Theories and @RunWith APIs.
custom_rules=[Shader.exclude_package('org.junit', recursive=True)])
custom_rules=[
Shader.exclude_package('org.junit', recursive=True),
Shader.exclude_package('org.hamcrest', recursive=True)
])

def __init__(self, task_exports, context):
self._task_exports = task_exports
Expand Down
10 changes: 10 additions & 0 deletions testprojects/tests/java/org/pantsbuild/testproject/matcher/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright 2015 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).


junit_tests(name='matcher',
sources=['MatcherTest.java'],
dependencies=[
'3rdparty:junit',
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2015 Pants project contributors (see CONTRIBUTORS.md).
// Licensed under the Apache License, Version 2.0 (see LICENSE).

package org.pantsbuild.testproject.matcher;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

/**
* This test insures that the junit and hamcrest classes work for a junit test.
*/
public class MatcherTest {

class FooMatcher extends BaseMatcher<String> {

@Override public boolean matches(Object o) {
assertTrue(String.class.isInstance(o));
String value = (String) o;
return value.contains("foo");
}

@Override public void describeTo(Description description) {
description.appendText(this.getClass().getSimpleName());
}
}

@Test
public void testMatcher() {
FooMatcher matcher = new FooMatcher();
assertTrue(matcher.matches("foobar"));
assertFalse(matcher.matches("Hello World!"));
}

/**
* This test fails if the org.hamcrest classes are shaded in junit runner
*/
@Test
public void testAssertThat() {
assertThat("Give me some food", containsString("foo"));
}
}

0 comments on commit 35a513b

Please sign in to comment.