Skip to content

Commit 106a358

Browse files
Configure Mockito to disable the Objenesis class cache (#273)
* Add test for Mockito meta test issue * Fix Mockito meta test issue by clearing ObjenesisBase instantiator cache when replacing the classloader * Add another meta test just to be sure * Revert "Fix Mockito meta test issue by clearing ObjenesisBase instantiator cache when replacing the classloader" This reverts commit ec6611c. * Disable Objenesis cache using a custom MockitoConfiguration
1 parent 6eb1299 commit 106a358

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.mockito.configuration;
2+
3+
// The presence of this class configures Mockito to disable the Objenesis cache.
4+
// Caching causes problems when we dynamically replace classes in meta tests.
5+
6+
@SuppressWarnings("unused")
7+
public class MockitoConfiguration extends DefaultMockitoConfiguration {
8+
@Override
9+
public boolean enableClassCache() {
10+
return false;
11+
}
12+
}

andy/src/test/java/integration/LibraryMetaTestsTest.java

+8
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,12 @@ void somePenaltyMetaTestFailingWithWeights() {
130130
.has(failedMetaTest("DoesNotApplyLastCarry"));
131131
}
132132

133+
@Test
134+
void metaTestsWithMockitoAndCustomException() {
135+
Result result = run("MockingAssignmentWithCustomExceptionLibrary", "MockingAssignmentWithCustomExceptionWrongWithoutAssertions", "MockingAssignmentWithCustomExceptionConfiguration");
136+
137+
assertThat(result.getMetaTests().getPassedMetaTests()).isEqualTo(0);
138+
assertThat(result.getMetaTests().getTotalTests()).isEqualTo(2);
139+
}
140+
133141
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package delft;
2+
3+
import nl.tudelft.cse1110.andy.codechecker.checks.Comparison;
4+
import nl.tudelft.cse1110.andy.codechecker.checks.MethodCalledAnywhere;
5+
import nl.tudelft.cse1110.andy.codechecker.checks.MockClass;
6+
import nl.tudelft.cse1110.andy.codechecker.checks.MockitoVerify;
7+
import nl.tudelft.cse1110.andy.codechecker.engine.AndCheck;
8+
import nl.tudelft.cse1110.andy.codechecker.engine.CheckScript;
9+
import nl.tudelft.cse1110.andy.codechecker.engine.SingleCheck;
10+
import nl.tudelft.cse1110.andy.config.MetaTest;
11+
import nl.tudelft.cse1110.andy.config.RunConfiguration;
12+
import nl.tudelft.cse1110.andy.execution.mode.Mode;
13+
14+
import java.util.HashMap;
15+
import java.util.List;
16+
import java.util.Map;
17+
18+
public class Configuration extends RunConfiguration {
19+
20+
@Override
21+
public Mode mode() {
22+
return Mode.GRADING;
23+
}
24+
25+
@Override
26+
public Map<String, Float> weights() {
27+
return new HashMap<>() {{
28+
put("coverage", 0.0f);
29+
put("mutation", 0.0f);
30+
put("meta", 1.0f);
31+
put("codechecks", 0.0f);
32+
}};
33+
}
34+
35+
@Override
36+
public List<String> classesUnderTest() {
37+
return List.of("delft.MyService");
38+
}
39+
40+
@Override
41+
public List<MetaTest> metaTests() {
42+
return List.of(
43+
MetaTest.withStringReplacement("my meta test",
44+
"a == 10",
45+
"a == 15"),
46+
MetaTest.withStringReplacement("another meta test",
47+
"a == 10",
48+
"a == 20")
49+
);
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package delft;
2+
3+
class MyService {
4+
private final AnotherService anotherService;
5+
6+
public MyService(AnotherService anotherService) {
7+
this.anotherService = anotherService;
8+
}
9+
10+
public boolean myMethod(int a) {
11+
if (a == 10) {
12+
return false;
13+
}
14+
15+
try {
16+
return anotherService.anotherMethod();
17+
} catch (MyException e) {
18+
return false;
19+
}
20+
}
21+
22+
}
23+
24+
interface AnotherService {
25+
boolean anotherMethod() throws MyException;
26+
}
27+
28+
class MyException extends Exception {
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package delft;
2+
3+
import static org.assertj.core.api.Assertions.*;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
import static org.mockito.ArgumentMatchers.*;
6+
import static org.mockito.Mockito.*;
7+
8+
import java.util.*;
9+
import java.time.*;
10+
import java.util.stream.*;
11+
12+
import org.junit.jupiter.api.*;
13+
import org.junit.jupiter.params.*;
14+
import org.junit.jupiter.params.provider.*;
15+
import org.mockito.*;
16+
17+
class MyServiceTest {
18+
19+
@Test
20+
void test() throws MyException {
21+
var anotherService = Mockito.mock(AnotherService.class);
22+
var myService = new MyService(anotherService);
23+
24+
when(anotherService.anotherMethod()).thenThrow(MyException.class);
25+
26+
boolean result = myService.myMethod(1);
27+
}
28+
29+
}

0 commit comments

Comments
 (0)