Skip to content

Commit

Permalink
Clarify ClassModifierShouldBe error message when dealing with package…
Browse files Browse the repository at this point in the history
… private classes.

Instead of an empty description (as per the JDK) AssertJ will describes such classes as package-private
Fixes assertj#1692
  • Loading branch information
joel-costigliola committed Dec 27, 2019
1 parent 82715ba commit ea4886b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
18 changes: 16 additions & 2 deletions src/main/java/org/assertj/core/error/ClassModifierShouldBe.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
*/
package org.assertj.core.error;

import static java.lang.String.format;
import static java.lang.reflect.Modifier.isPrivate;
import static java.lang.reflect.Modifier.isProtected;
import static java.lang.reflect.Modifier.isPublic;

import java.lang.reflect.Modifier;

/**
Expand All @@ -21,9 +26,11 @@
*/
public class ClassModifierShouldBe extends BasicErrorMessageFactory {

private static final String PACKAGE_PRIVATE = "package-private";

private ClassModifierShouldBe(Class<?> actual, boolean positive, String modifier) {
super("%nExpecting:%n <%s>%n" + (positive ? "to" : "not to") + " be a %s class but was %s.",
actual, modifier, Modifier.toString(actual.getModifiers()));
actual, modifier, modifiers(actual));
}

/**
Expand Down Expand Up @@ -73,7 +80,14 @@ public static ErrorMessageFactory shouldBeProtected(Class<?> actual) {
* @return the created {@code ErrorMessageFactory}.
*/
public static ErrorMessageFactory shouldBePackagePrivate(Class<?> actual) {
return new ClassModifierShouldBe(actual, true, "package-private");
return new ClassModifierShouldBe(actual, true, PACKAGE_PRIVATE);
}

private static String modifiers(Class<?> actual) {
int modifiers = actual.getModifiers();
boolean isPackagePrivate = !isPublic(modifiers) && !isProtected(modifiers) && !isPrivate(modifiers);
String modifiersDescription = Modifier.toString(modifiers);
return isPackagePrivate ? format("%s %s", PACKAGE_PRIVATE, modifiersDescription) : modifiersDescription;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,59 @@
package org.assertj.core.error;

import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.error.ClassModifierShouldBe.shouldBeFinal;
import static org.assertj.core.error.ClassModifierShouldBe.shouldBePublic;
import static org.assertj.core.error.ClassModifierShouldBe.shouldNotBeFinal;

import org.assertj.core.internal.TestDescription;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("ClassModifierShouldBe create")
class ClassModifierShouldBe_create_Test {

@Test
void should_create_error_message_for_is_final() {
String error = shouldBeFinal(Object.class).create(new TestDescription("TEST"));

assertThat(error).isEqualTo(format("[TEST] %n" +
"Expecting:%n" +
" <java.lang.Object>%n" +
"to be a \"final\" class but was \"public\"."));
// GIVEN
Class<Object> nonFinalClass = Object.class;
// WHEN
String error = shouldBeFinal(nonFinalClass).create(new TestDescription("TEST"));
// THEN
then(error).isEqualTo(format("[TEST] %n" +
"Expecting:%n" +
" <java.lang.Object>%n" +
"to be a \"final\" class but was \"public\"."));
}

@Test
void should_create_error_message_for_is_not_final() {
String error = shouldNotBeFinal(String.class).create(new TestDescription("TEST"));
// GIVEN
Class<String> finalClass = String.class;
// WHEN
String error = shouldNotBeFinal(finalClass).create(new TestDescription("TEST"));
// THEN
then(error).isEqualTo(format("[TEST] %n" +
"Expecting:%n" +
" <java.lang.String>%n" +
"not to be a \"final\" class but was \"public final\"."));
}

@Test
void should_create_clear_error_message_when_actual_is_package_private() {
// GIVEN
Class<PackagePrivate> packagePrivateClass = PackagePrivate.class;
// WHEN
String error = shouldBePublic(packagePrivateClass).create(new TestDescription("TEST"));
// THEN
then(error).isEqualTo(format("[TEST] %n" +
"Expecting:%n" +
" <org.assertj.core.error.ClassModifierShouldBe_create_Test.PackagePrivate>%n" +
"to be a \"public\" class but was \"package-private static final\"."));
}

assertThat(error).isEqualTo(format("[TEST] %n" +
"Expecting:%n" +
" <java.lang.String>%n" +
"not to be a \"final\" class but was \"public final\"."));
enum PackagePrivate {
MONITOR
}

}

0 comments on commit ea4886b

Please sign in to comment.