Skip to content

Commit

Permalink
Configure Mockito to disable the Objenesis class cache (#273)
Browse files Browse the repository at this point in the history
* 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
martinmladenov committed Jul 15, 2024
1 parent 6eb1299 commit 106a358
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 0 deletions.
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;
}
}
8 changes: 8 additions & 0 deletions andy/src/test/java/integration/LibraryMetaTestsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,12 @@ void somePenaltyMetaTestFailingWithWeights() {
.has(failedMetaTest("DoesNotApplyLastCarry"));
}

@Test
void metaTestsWithMockitoAndCustomException() {
Result result = run("MockingAssignmentWithCustomExceptionLibrary", "MockingAssignmentWithCustomExceptionWrongWithoutAssertions", "MockingAssignmentWithCustomExceptionConfiguration");

assertThat(result.getMetaTests().getPassedMetaTests()).isEqualTo(0);
assertThat(result.getMetaTests().getTotalTests()).isEqualTo(2);
}

}
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")
);
}
}
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 {
}
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);
}

}

0 comments on commit 106a358

Please sign in to comment.