Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Clean Unit test up (favor ExpectedException over try catch fail) #17

Merged
merged 1 commit into from
Dec 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,75 +1,54 @@
package com.pushtorefresh.private_constructor_checker;

import org.junit.Rule;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.rules.ExpectedException;

public class PrivateConstructionCheckerTest {
@Rule public ExpectedException expectedException = ExpectedException.none();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good approach


@Test
public void builderShouldThrowExceptionIfNullWasPassedAsExpectedTypeOfException() {
try {
PrivateConstructorChecker
.forClass(Object.class)
.expectedTypeOfException(null);

fail();
} catch (IllegalArgumentException expected) {
assertEquals("expectedTypeOfException can not be null", expected.getMessage());
}
PrivateConstructorChecker.Builder builder = PrivateConstructorChecker
.forClass(Object.class);

expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("expectedTypeOfException can not be null");
builder.expectedTypeOfException(null);
}

@Test
public void builderShouldThrowExceptionIfNullWasPassedAsExpectedExceptionMessage() {
try {
PrivateConstructorChecker
.forClass(Object.class)
.expectedExceptionMessage(null);

fail();
} catch (IllegalArgumentException expected) {
assertEquals("expectedExceptionMessage can not be null", expected.getMessage());
}
PrivateConstructorChecker.Builder builder = PrivateConstructorChecker
.forClass(Object.class);

expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("expectedExceptionMessage can not be null");
builder.expectedExceptionMessage(null);
}

@Test
public void builderShouldThrowExceptionIfNullWasPassedAsClass() {
try {
PrivateConstructorChecker
.forClass(null)
.check();

fail();
} catch (IllegalArgumentException expected) {
assertEquals("class can not be null", expected.getMessage());
}
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("class can not be null");
PrivateConstructorChecker
.forClass(null);
}

@Test
public void builderShouldThrowExceptionIfNullWasPassedAsClasses() {
try {
PrivateConstructorChecker
.forClasses(null)
.check();

fail();
} catch (IllegalArgumentException expected) {
assertEquals("classes can not be null or empty", expected.getMessage());
}
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("classes can not be null or empty");
PrivateConstructorChecker
.forClasses(null);
}

@Test
public void builderShouldThrowExceptionIfNullWasPassedAsOneOfClasses() {
try {
PrivateConstructorChecker
.forClasses(ClassWithPrivateConstructor.class, null)
.check();

fail();
} catch (IllegalArgumentException expected) {
assertEquals("class can not be null", expected.getMessage());
}
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("class can not be null");
PrivateConstructorChecker
.forClasses(ClassWithPrivateConstructor.class, null);
}

static class ClassWithoutDefaultConstructor {
Expand All @@ -79,18 +58,12 @@ private ClassWithoutDefaultConstructor(String someParam) {

@Test
public void shouldThrowExceptionIfClassHasNonDefaultConstructor() {
try {
PrivateConstructorChecker
.forClass(ClassWithoutDefaultConstructor.class)
.check();

fail();
} catch (AssertionError expected) {
assertEquals(
ClassWithoutDefaultConstructor.class + " has non-default constructor with some parameters",
expected.getMessage()
);
}
PrivateConstructorChecker.Builder builder = PrivateConstructorChecker
.forClass(ClassWithoutDefaultConstructor.class);

expectedException.expect(AssertionError.class);
expectedException.expectMessage(ClassWithoutDefaultConstructor.class + " has non-default constructor with some parameters");
builder.check();
}

static class ClassWithPrivateConstructor {
Expand All @@ -112,15 +85,12 @@ static class ClassWithDefaultProtectedConstructor {

@Test
public void shouldThrowExceptionBecauseConstructorHasDefaultModifier() {
try {
PrivateConstructorChecker
.forClass(ClassWithDefaultProtectedConstructor.class)
.check();

fail();
} catch (AssertionError expected) {
assertEquals("Constructor of " + ClassWithDefaultProtectedConstructor.class + " must be private", expected.getMessage());
}
PrivateConstructorChecker.Builder builder = PrivateConstructorChecker
.forClass(ClassWithDefaultProtectedConstructor.class);

expectedException.expect(AssertionError.class);
expectedException.expectMessage("Constructor of " + ClassWithDefaultProtectedConstructor.class + " must be private");
builder.check();
}

static class ClassWithProtectedConstructor {
Expand All @@ -130,15 +100,12 @@ protected ClassWithProtectedConstructor() {

@Test
public void shouldThrowExceptionBecauseConstructorHasProtectedModifier() {
try {
PrivateConstructorChecker
.forClass(ClassWithProtectedConstructor.class)
.check();

fail();
} catch (AssertionError expected) {
assertEquals("Constructor of " + ClassWithProtectedConstructor.class + " must be private", expected.getMessage());
}
PrivateConstructorChecker.Builder builder = PrivateConstructorChecker
.forClass(ClassWithProtectedConstructor.class);

expectedException.expect(AssertionError.class);
expectedException.expectMessage("Constructor of " + ClassWithProtectedConstructor.class + " must be private");
builder.check();
}

static class ClassWithPublicConstructor {
Expand All @@ -148,15 +115,12 @@ public ClassWithPublicConstructor() {

@Test
public void shouldThrowExceptionBecauseConstructorHasPublicModifier() {
try {
PrivateConstructorChecker
.forClass(ClassWithPublicConstructor.class)
.check();

fail();
} catch (AssertionError expected) {
assertEquals("Constructor of " + ClassWithPublicConstructor.class + " must be private", expected.getMessage());
}
PrivateConstructorChecker.Builder builder = PrivateConstructorChecker
.forClass(ClassWithPublicConstructor.class);

expectedException.expect(AssertionError.class);
expectedException.expectMessage("Constructor of " + ClassWithPublicConstructor.class + " must be private");
builder.check();
}

static class ClassWithConstructorThatThrowsException {
Expand Down Expand Up @@ -192,50 +156,37 @@ public void shouldCheckThatConstructorThrowsExceptionWithExpectedMessageButWitho

@Test
public void shouldThrowExceptionBecauseTypeOfExpectedExceptionDoesNotMatch() {
try {
PrivateConstructorChecker
.forClass(ClassWithConstructorThatThrowsException.class)
.expectedTypeOfException(IllegalArgumentException.class) // Incorrect type
.check();

fail();
} catch (IllegalStateException expected) {
assertEquals("For " + ClassWithConstructorThatThrowsException.class + " expected exception of type = class java.lang.IllegalArgumentException, " +
"but was exception of type = class java.lang.IllegalStateException",
expected.getMessage()
);
}
PrivateConstructorChecker.Builder builder = PrivateConstructorChecker
.forClass(ClassWithConstructorThatThrowsException.class)
.expectedTypeOfException(IllegalArgumentException.class); // Incorrect type

expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("For " + ClassWithConstructorThatThrowsException.class + " expected exception of type = class java.lang.IllegalArgumentException, " +
"but was exception of type = class java.lang.IllegalStateException");
builder.check();
}

@Test
public void shouldThrowExceptionBecauseExpectedMessageDoesNotMatch() {
try {
PrivateConstructorChecker
.forClass(ClassWithConstructorThatThrowsException.class)
.expectedTypeOfException(IllegalStateException.class) // Correct type
.expectedExceptionMessage("lol, not something that you've expected?") // Incorrect message
.check();

fail();
} catch (IllegalStateException expected) {
assertEquals("For " + ClassWithConstructorThatThrowsException.class + " expected exception message = 'lol, not something that you've expected?', " +
"but was = 'test exception'",
expected.getMessage()
);
}
PrivateConstructorChecker.Builder builder =PrivateConstructorChecker
.forClass(ClassWithConstructorThatThrowsException.class)
.expectedTypeOfException(IllegalStateException.class) // Correct type
.expectedExceptionMessage("lol, not something that you've expected?"); // Incorrect message

expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("For " + ClassWithConstructorThatThrowsException.class + " expected exception message = 'lol, not something that you've expected?', " +
"but was = 'test exception'");
builder.check();
}

@Test
public void shouldThrowExceptionBecauseConstructorThrownUnexpectedException() {
try {
PrivateConstructorChecker
.forClass(ClassWithConstructorThatThrowsException.class)
.check(); // We don't expect exception, but it will be thrown

fail();
} catch (IllegalStateException expected) {
assertEquals("For " + ClassWithConstructorThatThrowsException.class + " no exception was expected", expected.getMessage());
}
PrivateConstructorChecker.Builder builder = PrivateConstructorChecker
.forClass(ClassWithConstructorThatThrowsException.class);

expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("For " + ClassWithConstructorThatThrowsException.class + " no exception was expected");
builder.check(); // We don't expect exception, but it will be thrown
}

static class ClassWith2Constructors {
Expand All @@ -252,15 +203,12 @@ private ClassWith2Constructors(String str) {

@Test
public void shouldThrowExceptionBecauseClassHasMoreThanOneConstructor() {
try {
PrivateConstructorChecker
.forClass(ClassWith2Constructors.class)
.check();

fail();
} catch (AssertionError expected) {
assertEquals(ClassWith2Constructors.class + " has more than one constructor", expected.getMessage());
}
PrivateConstructorChecker.Builder builder = PrivateConstructorChecker
.forClass(ClassWith2Constructors.class);

expectedException.expect(AssertionError.class);
expectedException.expectMessage(ClassWith2Constructors.class + " has more than one constructor");
builder.check();
}

static class ClassWithoutDeclaredConstructor {
Expand All @@ -269,15 +217,12 @@ static class ClassWithoutDeclaredConstructor {

@Test
public void shouldThrowExceptionBecauseClassDoesNotHaveDeclaredConstructors() {
try {
PrivateConstructorChecker
.forClass(ClassWithoutDeclaredConstructor.class)
.check();

fail();
} catch (AssertionError expected) {
assertEquals("Constructor of " + ClassWithoutDeclaredConstructor.class + " must be private", expected.getMessage());
}
PrivateConstructorChecker.Builder builder = PrivateConstructorChecker
.forClass(ClassWithoutDeclaredConstructor.class);

expectedException.expect(AssertionError.class);
expectedException.expectMessage("Constructor of " + ClassWithoutDeclaredConstructor.class + " must be private");
builder.check();
}

static class AnotherClassWithConstructorThatThrowsException {
Expand All @@ -297,17 +242,11 @@ public void shouldCheckMultipleThatConstructorThrowsExceptionWithExpectedMessage

@Test
public void shouldThrowExceptionIfOneOfClassesHasNonDefaultConstructor() {
try {
PrivateConstructorChecker
.forClasses(ClassWithPrivateConstructor.class, ClassWithoutDefaultConstructor.class)
.check();

fail();
} catch (AssertionError expected) {
assertEquals(
ClassWithoutDefaultConstructor.class + " has non-default constructor with some parameters",
expected.getMessage()
);
}
PrivateConstructorChecker.Builder builder = PrivateConstructorChecker
.forClasses(ClassWithPrivateConstructor.class, ClassWithoutDefaultConstructor.class);

expectedException.expect(AssertionError.class);
expectedException.expectMessage(ClassWithoutDefaultConstructor.class + " has non-default constructor with some parameters");
builder.check();
}
}