Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure Mockito to disable the Objenesis class cache #273

Merged
merged 5 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}
Loading