Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fixes #2510: Remove ExpectedException from internal test suite #2518

Merged
merged 72 commits into from Dec 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
90115c7
Replace ExpectedException in AtLeastXNumberOfInvocationsCheckerTest
temp-droid Dec 14, 2021
8be75e6
Replace ExpectedException in TimesTest
temp-droid Dec 14, 2021
bba18bc
Replace ExpectedException in VerificationOverTimeImplTest
temp-droid Dec 14, 2021
5d3e6f4
Replace ExpectedException in MissingInvocationCheckerTest
temp-droid Dec 14, 2021
5b2fe7b
Replace ExpectedException in MockInjectionUsingConstructorTest
temp-droid Dec 15, 2021
fdcec50
Replace ExpectedException in VarargsTest
temp-droid Dec 15, 2021
30b1a6d
Replace ExpectedException in StubbingWithThrowablesTest
temp-droid Dec 15, 2021
a93598a
Replace ExpectedException in VerificationInOrderWithCallsTest
temp-droid Dec 16, 2021
35b3a0b
Reformat previously touched files
temp-droid Dec 16, 2021
36fba92
Replace ExpectedException in DescriptionTest
temp-droid Dec 16, 2021
2c30062
Replace ExpectedException in ChecksTest
temp-droid Dec 16, 2021
baeaa77
Replace ExpectedException in TimerTest
temp-droid Dec 16, 2021
f034dee
Replace ExpectedException in NegativeDurationTest
temp-droid Dec 16, 2021
bf50938
Replace ExpectedException in NumberOfInvocationsInOrderCheckerTest
temp-droid Dec 16, 2021
054a01c
Replace ExpectedException in NumberOfInvocationsCheckerTest
temp-droid Dec 16, 2021
58b7ece
Replace @Test(expected=...) in ConstructionMockTest
temp-droid Dec 17, 2021
08c15fa
Replace @Test(expected=...) in StaticMockTest
temp-droid Dec 17, 2021
8dad4e8
Replace @Test(expected=...) in MocksTest
temp-droid Dec 17, 2021
a271c76
Replace @Test(expected=...) in MockitoTest
temp-droid Dec 17, 2021
96a36e2
Fix actual & expected order in last MockitoTest assertion
temp-droid Dec 17, 2021
21eb375
Replace @Test(expected=...) in MockAnnotationProcessorTest
temp-droid Dec 18, 2021
0f2b920
Replace @Test(expected=...) in MockInjectionTest
temp-droid Dec 18, 2021
3660c41
Replace `isEqualTo(true)` with `isTrue()`
temp-droid Dec 18, 2021
cd6dfe1
Replace @Test(expected=...) in MockSettingsImplTest
temp-droid Dec 18, 2021
d2e47eb
Replace junit assertions with assertj
temp-droid Dec 18, 2021
982b56c
Replace @Test(expected=...) in ConstructorInstantiatorTest
temp-droid Dec 18, 2021
cb96576
Replace junit assertions with assertj
temp-droid Dec 18, 2021
5f6a321
Replace @Test(expected=...) in ReporterTest
temp-droid Dec 18, 2021
c520d44
Replace @Test(expected=...) in DefaultMockitoFrameworkTest
temp-droid Dec 18, 2021
e5a65f6
Replace @Test(expected=...) in AnswersWithDelayTest
temp-droid Dec 18, 2021
19a2c00
Replace @Test(expected=...) in DoesNothingTest
temp-droid Dec 18, 2021
628c234
Replace @Test(expected=...) in ReturnsTest
temp-droid Dec 18, 2021
33098b7
Replace @Test(expected=...) in ThrowsExceptionTest
temp-droid Dec 18, 2021
2a7b4a0
Replace @Test(expected=...) in DefaultMockingDetailsTest
temp-droid Dec 20, 2021
4b9530d
Replace @Test(expected=...) in FieldInitializerTest
temp-droid Dec 20, 2021
ba473ae
Replace @Test(expected=...) in HashCodeAndEqualsSafeSetTest
temp-droid Dec 20, 2021
a30ca41
Replace @Test(expected=...) in MockCreationValidatorTest
temp-droid Dec 20, 2021
e219fc6
Replace @Test(expected=...) in MockInjectionUsingConstructorTest
temp-droid Dec 20, 2021
2fa9b71
Replace @Test(expected=...) in MockUtilTest
temp-droid Dec 20, 2021
42cbd0e
Replace @Test(expected=...) in ResetInvocationsTest
temp-droid Dec 20, 2021
b822744
Replace @Test(expected=...) in ResetTest
temp-droid Dec 20, 2021
2336b7d
Replace @Test(expected=...) in ReturnsGenericDeepStubsTest
temp-droid Dec 20, 2021
f483842
Replace @Test(expected=...) in SpyAnnotationTest
temp-droid Dec 20, 2021
4cedd6c
Replace @Test(expected=...) in WrongSetOfAnnotationsTest
temp-droid Dec 20, 2021
d89abae
Remove unused imports in MockInjectionUsingConstructorTest
temp-droid Dec 20, 2021
39a1ab6
Fix test in SpyAnnotationTest
temp-droid Dec 20, 2021
baa5366
Fix test in ReturnsGenericDeepStubsTest
temp-droid Dec 20, 2021
20108c5
Replace @Test(expected=...) in ClassCastExOnVerifyZeroInteractionsTest
temp-droid Dec 20, 2021
93d8c01
Replace @Test(expected=...) in NPEWithCertainMatchersTest
temp-droid Dec 20, 2021
6eab0df
Replace @Test(expected=...) in BDDMockitoTest
temp-droid Dec 20, 2021
b1c5c7b
Remove @Test(expected=...) in LenientJUnitRuleTest
temp-droid Dec 20, 2021
3417f23
Replace @Test(expected=...) in VerificationCollectorImplTest
temp-droid Dec 20, 2021
a100d94
Remove unused import in VerificationCollectorImplTest
temp-droid Dec 20, 2021
7627fdf
Replace @Test(expected=...) in MatchersTest
temp-droid Dec 21, 2021
db0981b
Replace @Test(expected=...) in ReflectionMatchersTest
temp-droid Dec 21, 2021
a348c26
Replace @Test(expected=...) in DescriptiveMessagesOnMisuseTest
temp-droid Dec 21, 2021
75ccfa3
Replace @Test(expected=...) in InvalidUsageTest
temp-droid Dec 21, 2021
695021d
Replace @Test(expected=...) in StubbingConsecutiveAnswersTest
temp-droid Dec 21, 2021
2efc8a2
Replace @Test(expected=...) in StubbingUsingDoReturnTest
temp-droid Dec 21, 2021
1b4f335
Replace @Test(expected=...) in StubbingWarningsTest
temp-droid Dec 21, 2021
291a04f
Replace @Test(expected=...) in BasicVerificationInOrderTest
temp-droid Dec 21, 2021
c65c55c
Replace @Test(expected=...) in BasicVerificationTest
temp-droid Dec 21, 2021
2dd30c7
Replace @Test(expected=...) in NoMoreInteractionsVerificationTest
temp-droid Dec 21, 2021
0dd975c
Replace @Test(expected=...) in RelaxedVerificationInOrderTest
temp-droid Dec 21, 2021
67436f4
Replace @Test(expected=...) in VerificationExcludingStubsTest
temp-droid Dec 21, 2021
d9edde1
Replace @Test(expected=...) in VerificationInOrderMixedWithOrdiraryVe…
temp-droid Dec 21, 2021
02e57ef
Rename VerificationInOrderMixedWithOrdiraryVerificationTest to Verifi…
temp-droid Dec 21, 2021
797d0a6
Replace @Test(expected=...) in ClassLoadersTest
temp-droid Dec 21, 2021
c2c73be
Replace @Test(expected=...) in SafeJUnitRuleTest
temp-droid Dec 21, 2021
f5e895d
Replace @Test(expected=...) in MockHandlerImplTest
temp-droid Dec 21, 2021
dc2231c
Replace @Test(expected=...) in AtLeastMarksAllInvocationsVerified
temp-droid Dec 21, 2021
367fb12
Replace @Test(expected=...) in InvocationNotifierHandlerTest
temp-droid Dec 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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