Skip to content

Commit c31323f

Browse files
committed
TG-23051 Specifying throwException and throwExceptionFactory for @InTestsMock when the mocked method is called
1 parent 6b963b3 commit c31323f

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/main/java/com/diffblue/cover/annotations/InTestsMock.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static java.lang.annotation.ElementType.TYPE;
2121
import static java.lang.annotation.RetentionPolicy.RUNTIME;
2222

23+
import com.diffblue.cover.annotations.exceptions.NoException;
2324
import java.lang.annotation.Repeatable;
2425
import java.lang.annotation.Retention;
2526
import java.lang.annotation.Target;
@@ -87,4 +88,16 @@
8788
* #method()}
8889
*/
8990
String returnValueFactory() default "";
91+
92+
/**
93+
* @return exception type to throw when the mocked {@link #method()} is called. Defaults to {@link
94+
* NoException} to indicate no exception should be thrown.
95+
*/
96+
Class<? extends Throwable> throwException() default NoException.class;
97+
98+
/**
99+
* @return fully qualified name of the factory method used to create the exception to throw when
100+
* the mocked {@link #method()} is called.
101+
*/
102+
String throwExceptionFactory() default "";
90103
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2025 Diffblue Limited.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
package com.diffblue.cover.annotations.exceptions;
16+
17+
/**
18+
* Marker class used to indicate that no exception should be thrown by default in {@link
19+
* com.diffblue.cover.annotations.InTestsMock#throwException()}.
20+
*/
21+
public final class NoException extends Throwable {
22+
private NoException() {
23+
// This class should not be instantiated.
24+
}
25+
}

0 commit comments

Comments
 (0)