Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0309e91

Browse files
Fixes unchecked warnings when calling Mockito.mock(Class)
This is an issue well known by the Mockito community, but prone to notbefixed:mockito/mockito#1531Generics allow to ensure type-safety at compile time instead of runtime.However, here it is used mainly to auto-cast the created object, thusavoiding this burden on the developer. Indeed, it is in a context wheretype safety is usually not a requirement, since the use of this methodis often the most trivial we can have: provide a class and expect a veryinstance of this class in return.This commit thus creates a less constrained mock method which builds onMockito's one, but without the constraint inducing this warning.
1 parent73d9c4f commit0309e91

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

‎javaparser-core-testing/src/test/java/com/github/javaparser/ast/visitor/GenericListVisitorAdapterTest.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
importjava.util.Optional;
3939

4040
importstaticorg.junit.jupiter.api.Assertions.assertNotNull;
41-
importstaticorg.mockito.Mockito.mock;
4241
importstaticorg.mockito.Mockito.times;
4342

4443
classGenericListVisitorAdapterTest {
@@ -2550,4 +2549,10 @@ void visit_CompactConstructorDeclaration () {
25502549
order.verifyNoMoreInteractions();
25512550
}
25522551

2552+
@SuppressWarnings("unchecked")
2553+
// Non type-safe mock method to avoid unchecked warnings
2554+
// Its use is trivial and systematic enough to not be a problem
2555+
private <T>Tmock(Class<?>classToMock) {
2556+
return (T)Mockito.mock(classToMock);
2557+
}
25532558
}

‎javaparser-core-testing/src/test/java/com/github/javaparser/ast/visitor/GenericVisitorAdapterTest.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
importjava.util.Optional;
3838

3939
importstaticorg.junit.jupiter.api.Assertions.assertNull;
40-
importstaticorg.mockito.Mockito.mock;
4140
importstaticorg.mockito.Mockito.times;
4241

4342
publicclassGenericVisitorAdapterTest {
@@ -2549,4 +2548,10 @@ void visit_CompactConstructorDeclaration () {
25492548
order.verifyNoMoreInteractions();
25502549
}
25512550

2551+
@SuppressWarnings("unchecked")
2552+
// Non type-safe mock method to avoid unchecked warnings
2553+
// Its use is trivial and systematic enough to not be a problem
2554+
private <T>Tmock(Class<?>classToMock) {
2555+
return (T)Mockito.mock(classToMock);
2556+
}
25522557
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp