-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
6eb1299
commit 106a358
Showing
5 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
andy/src/main/java/org/mockito/configuration/MockitoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.mockito.configuration; | ||
|
||
// The presence of this class configures Mockito to disable the Objenesis cache. | ||
// Caching causes problems when we dynamically replace classes in meta tests. | ||
|
||
@SuppressWarnings("unused") | ||
public class MockitoConfiguration extends DefaultMockitoConfiguration { | ||
@Override | ||
public boolean enableClassCache() { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...t/resources/grader/fixtures/Config/MockingAssignmentWithCustomExceptionConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package delft; | ||
|
||
import nl.tudelft.cse1110.andy.codechecker.checks.Comparison; | ||
import nl.tudelft.cse1110.andy.codechecker.checks.MethodCalledAnywhere; | ||
import nl.tudelft.cse1110.andy.codechecker.checks.MockClass; | ||
import nl.tudelft.cse1110.andy.codechecker.checks.MockitoVerify; | ||
import nl.tudelft.cse1110.andy.codechecker.engine.AndCheck; | ||
import nl.tudelft.cse1110.andy.codechecker.engine.CheckScript; | ||
import nl.tudelft.cse1110.andy.codechecker.engine.SingleCheck; | ||
import nl.tudelft.cse1110.andy.config.MetaTest; | ||
import nl.tudelft.cse1110.andy.config.RunConfiguration; | ||
import nl.tudelft.cse1110.andy.execution.mode.Mode; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Configuration extends RunConfiguration { | ||
|
||
@Override | ||
public Mode mode() { | ||
return Mode.GRADING; | ||
} | ||
|
||
@Override | ||
public Map<String, Float> weights() { | ||
return new HashMap<>() {{ | ||
put("coverage", 0.0f); | ||
put("mutation", 0.0f); | ||
put("meta", 1.0f); | ||
put("codechecks", 0.0f); | ||
}}; | ||
} | ||
|
||
@Override | ||
public List<String> classesUnderTest() { | ||
return List.of("delft.MyService"); | ||
} | ||
|
||
@Override | ||
public List<MetaTest> metaTests() { | ||
return List.of( | ||
MetaTest.withStringReplacement("my meta test", | ||
"a == 10", | ||
"a == 15"), | ||
MetaTest.withStringReplacement("another meta test", | ||
"a == 10", | ||
"a == 20") | ||
); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...c/test/resources/grader/fixtures/Library/MockingAssignmentWithCustomExceptionLibrary.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package delft; | ||
|
||
class MyService { | ||
private final AnotherService anotherService; | ||
|
||
public MyService(AnotherService anotherService) { | ||
this.anotherService = anotherService; | ||
} | ||
|
||
public boolean myMethod(int a) { | ||
if (a == 10) { | ||
return false; | ||
} | ||
|
||
try { | ||
return anotherService.anotherMethod(); | ||
} catch (MyException e) { | ||
return false; | ||
} | ||
} | ||
|
||
} | ||
|
||
interface AnotherService { | ||
boolean anotherMethod() throws MyException; | ||
} | ||
|
||
class MyException extends Exception { | ||
} |
29 changes: 29 additions & 0 deletions
29
.../grader/fixtures/Solution/MockingAssignmentWithCustomExceptionWrongWithoutAssertions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package delft; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import java.util.*; | ||
import java.time.*; | ||
import java.util.stream.*; | ||
|
||
import org.junit.jupiter.api.*; | ||
import org.junit.jupiter.params.*; | ||
import org.junit.jupiter.params.provider.*; | ||
import org.mockito.*; | ||
|
||
class MyServiceTest { | ||
|
||
@Test | ||
void test() throws MyException { | ||
var anotherService = Mockito.mock(AnotherService.class); | ||
var myService = new MyService(anotherService); | ||
|
||
when(anotherService.anotherMethod()).thenThrow(MyException.class); | ||
|
||
boolean result = myService.myMethod(1); | ||
} | ||
|
||
} |