Skip to content

Commit

Permalink
Ensured that the tests check that the class definition has the approp…
Browse files Browse the repository at this point in the history
…riate modifiers.
  • Loading branch information
nicul committed Oct 15, 2019
1 parent c8a8793 commit 7e05b54
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/java/MyFirstImmutableEntity.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public class MyFirstImmutableEntity {
public final class MyFirstImmutableEntity {

private final int myFirstImmutableField;

Expand Down
14 changes: 12 additions & 2 deletions src/test/java/MyFirstImmutableEntityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,23 @@ public class MyFirstImmutableEntityTest {

@Test
public void shouldHaveCreatedPublicFinalClassWithoutAnyPackage() {
assertClassExistsInDefaultPackage();
final Class<?> myFirstImmutableClass = assertClassExistsInDefaultPackage();

if(!Modifier.isPublic(myFirstImmutableClass.getModifiers())) {
fail("The class " + CLASS_NAME + " has not been declared 'public'");
}

if(!Modifier.isFinal(myFirstImmutableClass.getModifiers())) {
fail("The class " + CLASS_NAME + " has not been declared 'final'");
}
}

@Test
public void shouldHaveAddedPrivateFinalIntFieldToTheClass() {
final Class<?> myFirstImmutableClass = assertClassExistsInDefaultPackage();

final Field field = assertImmutableFieldExistsInClass(myFirstImmutableClass);

if(!field.getType().equals(int.class)) {
fail("The field " + FIELD_NAME + " has been found in " + CLASS_NAME +
" but it is of type " + field.getType() + " rather than 'int'");
Expand All @@ -43,7 +53,7 @@ private Class<?> assertClassExistsInDefaultPackage() {
try {
return Class.forName(CLASS_NAME);
} catch (ClassNotFoundException e) {
fail("The class " + CLASS_NAME + " has not been found in the default package.");
fail("The class " + CLASS_NAME + " has not been found in the default package");
return null;
}
}
Expand Down

0 comments on commit 7e05b54

Please sign in to comment.