Skip to content

Commit

Permalink
Fixes mockito#2510: Remove ExpectedException from internal test suite (
Browse files Browse the repository at this point in the history
  • Loading branch information
temp-droid authored and xuelin2020 committed Dec 28, 2021
1 parent d109d4c commit 3d53496
Show file tree
Hide file tree
Showing 63 changed files with 2,440 additions and 1,364 deletions.
Expand Up @@ -115,7 +115,7 @@ public void run() {
DescriptiveMessagesWhenVerificationFailsTest.class,
DescriptiveMessagesWhenTimesXVerificationFailsTest.class,
BasicVerificationInOrderTest.class,
VerificationInOrderMixedWithOrdiraryVerificationTest.class,
VerificationInOrderMixedWithOrdinaryVerificationTest.class,
DescriptiveMessagesOnVerificationInOrderErrorsTest.class,
InvalidStateDetectionTest.class,
ReplacingObjectMethodsTest.class,
Expand Down
28 changes: 18 additions & 10 deletions src/test/java/org/mockito/DescriptionTest.java
Expand Up @@ -4,42 +4,50 @@
*/
package org.mockito;

import static org.mockito.Mockito.*;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.description;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.exceptions.base.MockitoAssertionError;

/**
* Tests for https://github.com/mockito/mockito/issues/1712
*/
public class DescriptionTest {
@Rule public ExpectedException expectedException = ExpectedException.none();

@Test
public void verify_method_not_called_should_include_description_in_report() {
final String description = "Failed to call doSomethingElse";
expectedException.expect(MockitoAssertionError.class);
expectedException.expectMessage(description);

Dependency dependency = spy(Dependency.class);
SystemUnderTest systemUnderTest = new SystemUnderTest();
systemUnderTest.doNothing(dependency);
verify(dependency, description(description)).doSomethingElse(false);

assertThatThrownBy(
() -> {
verify(dependency, description(description)).doSomethingElse(false);
})
.isInstanceOf(MockitoAssertionError.class)
.hasMessageContaining(description);
}

@Test
public void
verify_method_called_with_unexpected_argument_should_include_description_in_report() {
final String description = "Failed to call doSomethingElse with expected argument";
expectedException.expect(MockitoAssertionError.class);
expectedException.expectMessage(description);

Dependency dependency = spy(Dependency.class);
SystemUnderTest systemUnderTest = new SystemUnderTest();
systemUnderTest.doSomething(dependency);
verify(dependency, description(description)).doSomethingElse(false);

assertThatThrownBy(
() -> {
verify(dependency, description(description)).doSomethingElse(false);
})
.isInstanceOf(MockitoAssertionError.class)
.hasMessageContaining(description);
}

static class SystemUnderTest {
Expand Down
95 changes: 74 additions & 21 deletions src/test/java/org/mockito/MockitoTest.java
Expand Up @@ -5,6 +5,7 @@
package org.mockito;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.times;
import static org.mockito.internal.progress.ThreadSafeMockingProgress.mockingProgress;

Expand All @@ -28,56 +29,108 @@ public void shouldRemoveStubbableFromProgressAfterStubbing() {
}

@SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
@Test(expected = NotAMockException.class)
@Test
public void shouldValidateMockWhenVerifying() {
Mockito.verify("notMock");
assertThatThrownBy(
() -> {
Mockito.verify("notMock");
})
.isInstanceOf(NotAMockException.class)
.hasMessageContaining(
"Argument passed to verify() is of type String and is not a mock!")
.hasMessageContaining("Make sure you place the parenthesis correctly!");
}

@SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
@Test(expected = NotAMockException.class)
@Test
public void shouldValidateMockWhenVerifyingWithExpectedNumberOfInvocations() {
Mockito.verify("notMock", times(19));
assertThatThrownBy(
() -> {
Mockito.verify("notMock", times(19));
})
.isInstanceOf(NotAMockException.class)
.hasMessageContaining(
"Argument passed to verify() is of type String and is not a mock!")
.hasMessageContaining("Make sure you place the parenthesis correctly!");
}

@Test(expected = NotAMockException.class)
@Test
public void shouldValidateMockWhenVerifyingNoMoreInteractions() {
Mockito.verifyNoMoreInteractions("notMock");
assertThatThrownBy(
() -> {
Mockito.verifyNoMoreInteractions("notMock");
})
.isInstanceOf(NotAMockException.class)
.hasMessageContaining("Argument(s) passed is not a mock!");
}

@Test(expected = NotAMockException.class)
@Test
public void shouldValidateMockWhenVerifyingNoInteractions() {
Mockito.verifyNoInteractions("notMock");
assertThatThrownBy(
() -> {
Mockito.verifyNoInteractions("notMock");
})
.isInstanceOf(NotAMockException.class)
.hasMessageContaining("Argument(s) passed is not a mock!");
}

@Test(expected = NullInsteadOfMockException.class)
@Test
public void shouldValidateNullMockWhenVerifyingNoInteractions() {
Mockito.verifyNoInteractions(new Object[] {null});
assertThatThrownBy(
() -> {
Mockito.verifyNoInteractions(new Object[] {null});
})
.isInstanceOf(NullInsteadOfMockException.class)
.hasMessageContaining("Argument(s) passed is null!");
}

@SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
@Test(expected = NotAMockException.class)
@Test
public void shouldValidateMockWhenCreatingInOrderObject() {
Mockito.inOrder("notMock");
assertThatThrownBy(
() -> {
Mockito.inOrder("notMock");
})
.isInstanceOf(NotAMockException.class)
.hasMessageContaining("Argument(s) passed is not a mock!");
}

@SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
@Test(expected = MockitoException.class)
public void shouldGiveExplantionOnStaticMockingWithoutInlineMockMaker() {
Mockito.mockStatic(Object.class);
@Test
public void shouldGiveExplanationOnStaticMockingWithoutInlineMockMaker() {
assertThatThrownBy(
() -> {
Mockito.mockStatic(Object.class);
})
.isInstanceOf(MockitoException.class)
.hasMessageContainingAll(
"The used MockMaker SubclassByteBuddyMockMaker does not support the creation of static mocks",
"Mockito's inline mock maker supports static mocks based on the Instrumentation API.",
"You can simply enable this mock mode, by placing the 'mockito-inline' artifact where you are currently using 'mockito-core'.",
"Note that Mockito's inline mock maker is not supported on Android.");
}

@SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
@Test(expected = MockitoException.class)
public void shouldGiveExplantionOnConstructionMockingWithoutInlineMockMaker() {
Mockito.mockConstruction(Object.class);
@Test
public void shouldGiveExplanationOnConstructionMockingWithoutInlineMockMaker() {
assertThatThrownBy(
() -> {
Mockito.mockConstruction(Object.class);
})
.isInstanceOf(MockitoException.class)
.hasMessageContainingAll(
"The used MockMaker SubclassByteBuddyMockMaker does not support the creation of construction mocks",
"Mockito's inline mock maker supports construction mocks based on the Instrumentation API.",
"You can simply enable this mock mode, by placing the 'mockito-inline' artifact where you are currently using 'mockito-core'.",
"Note that Mockito's inline mock maker is not supported on Android.");
}

@Test
public void shouldStartingMockSettingsContainDefaultBehavior() {
// when
// given
MockSettingsImpl<?> settings = (MockSettingsImpl<?>) Mockito.withSettings();

// then
assertThat(Mockito.RETURNS_DEFAULTS).isEqualTo(settings.getDefaultAnswer());
// when / then
assertThat(settings.getDefaultAnswer()).isEqualTo(Mockito.RETURNS_DEFAULTS);
}
}
Expand Up @@ -4,14 +4,15 @@
*/
package org.mockito.internal.configuration;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.util.List;

import org.junit.Test;
import org.mockito.MockedStatic;
import org.mockito.exceptions.base.MockitoException;

import static org.assertj.core.api.Assertions.*;

public class MockAnnotationProcessorTest {

@SuppressWarnings("unused")
Expand All @@ -35,19 +36,34 @@ public void testNonGeneric() throws Exception {
assertThat(type).isEqualTo(Void.class);
}

@Test(expected = MockitoException.class)
public void testGeneric() throws Exception {
MockAnnotationProcessor.inferParameterizedType(
MockAnnotationProcessorTest.class.getDeclaredField("generic").getGenericType(),
"generic",
"Sample");
@Test
public void testGeneric() {
assertThatThrownBy(
() -> {
MockAnnotationProcessor.inferParameterizedType(
MockAnnotationProcessorTest.class
.getDeclaredField("generic")
.getGenericType(),
"generic",
"Sample");
})
.isInstanceOf(MockitoException.class)
.hasMessageContaining(
"Mockito cannot infer a static mock from a raw type for generic");
}

@Test(expected = MockitoException.class)
public void testRaw() throws Exception {
MockAnnotationProcessor.inferParameterizedType(
MockAnnotationProcessorTest.class.getDeclaredField("raw").getGenericType(),
"raw",
"Sample");
@Test
public void testRaw() {
assertThatThrownBy(
() -> {
MockAnnotationProcessor.inferParameterizedType(
MockAnnotationProcessorTest.class
.getDeclaredField("raw")
.getGenericType(),
"raw",
"Sample");
})
.isInstanceOf(MockitoException.class)
.hasMessageContaining("Mockito cannot infer a static mock from a raw type for raw");
}
}
Expand Up @@ -5,6 +5,7 @@
package org.mockito.internal.configuration;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;

import java.lang.reflect.Field;
Expand All @@ -28,24 +29,44 @@ public void reset() throws Exception {
withoutConstructor = null;
}

@Test(expected = IllegalArgumentException.class)
@Test
public void should_not_allow_null_on_field() {
MockInjection.onField((Field) null, this);
assertThatThrownBy(
() -> {
MockInjection.onField((Field) null, this);
})
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("item in fields should not be null");
}

@Test(expected = IllegalArgumentException.class)
@Test
public void should_not_allow_null_on_fields() {
MockInjection.onFields((Set<Field>) null, this);
assertThatThrownBy(
() -> {
MockInjection.onFields((Set<Field>) null, this);
})
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("fields should not be null");
}

@Test(expected = IllegalArgumentException.class)
public void should_not_allow_null_on_instance_owning_the_field() throws Exception {
MockInjection.onField(field("withConstructor"), null);
@Test
public void should_not_allow_null_on_instance_owning_the_field() {
assertThatThrownBy(
() -> {
MockInjection.onField(field("withConstructor"), null);
})
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("fieldOwner should not be null");
}

@Test(expected = IllegalArgumentException.class)
public void should_not_allow_null_on_mocks() throws Exception {
MockInjection.onField(field("withConstructor"), this).withMocks(null);
@Test
public void should_not_allow_null_on_mocks() {
assertThatThrownBy(
() -> {
MockInjection.onField(field("withConstructor"), this).withMocks(null);
})
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("mocks should not be null");
}

@Test
Expand All @@ -55,7 +76,7 @@ public void can_try_constructor_injection() throws Exception {
.tryConstructorInjection()
.apply();

assertThat(withConstructor.initializedWithConstructor).isEqualTo(true);
assertThat(withConstructor.initializedWithConstructor).isTrue();
}

@Test
Expand Down

0 comments on commit 3d53496

Please sign in to comment.