Skip to content

Commit

Permalink
#256: Avoid lenient mocks (#362)
Browse files Browse the repository at this point in the history
* #342: Restructure changelog

* Apply suggestions from code review

Co-authored-by: Christoph Pirkl <[email protected]>

* #342: Restructure changelog

* #342: Updated versions.

* #359: Improve logo transparency

* #359: Updated version number and changelog.

* #356: Fix warning in codeql-analysis.yml

* Upgrade test dependencies

* #246: Avoid configuring mocks as lenient

* Fix warning under Java 17

OpenJDK 64-Bit Server VM warning: Ignoring option --illegal-access=deny; support was removed in 17.0

* Test with Java 19

* Fix integration test

* Fix compiler warnings

* Skip tests that require the SecurityManager

Java 19ff does not support the SecurityManager any more.

* Update release date

---------

Co-authored-by: Sebastian Bär <[email protected]>
Co-authored-by: kaklakariada <[email protected]>
Co-authored-by: kaklakariada <[email protected]>
  • Loading branch information
4 people authored Dec 16, 2023
1 parent 384ab82 commit 6466a4b
Show file tree
Hide file tree
Showing 24 changed files with 170 additions and 180 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- os: ubuntu-latest
java: 17
- os: ubuntu-latest
java: 18
java: 19

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-os-${{ matrix.os }}-java-${{ matrix.java }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import static org.itsallcode.openfasttrace.api.core.SampleArtifactTypes.*;
import static org.itsallcode.openfasttrace.api.core.SpecificationItemAssertions.*;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;

import java.util.Arrays;
import java.util.List;
Expand All @@ -18,12 +17,9 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

// [utest->dsn~linked-specification-item~1]
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class TestLinkedSpecificationItem
{
private LinkedSpecificationItem linkedItem;
Expand All @@ -39,10 +35,10 @@ public void prepareAllTests()
this.linkedItem = new LinkedSpecificationItem(this.itemMock);
this.coveredLinkedItem = new LinkedSpecificationItem(this.coveredItemMock);
this.otherLinkedItem = new LinkedSpecificationItem(this.otherItemMock);
when(this.itemMock.getStatus()).thenReturn(ItemStatus.APPROVED);
when(this.itemMock.getId()).thenReturn(SpecificationItemId.parseId("a~item~1"));
when(this.coveredItemMock.getId()).thenReturn(SpecificationItemId.parseId("a~covered~1"));
when(this.otherItemMock.getId()).thenReturn(SpecificationItemId.parseId("a~other~1"));
lenient().when(this.itemMock.getStatus()).thenReturn(ItemStatus.APPROVED);
lenient().when(this.itemMock.getId()).thenReturn(SpecificationItemId.parseId("a~item~1"));
lenient().when(this.coveredItemMock.getId()).thenReturn(SpecificationItemId.parseId("a~covered~1"));
lenient().when(this.otherItemMock.getId()).thenReturn(SpecificationItemId.parseId("a~other~1"));
}

@Test
Expand Down Expand Up @@ -136,7 +132,6 @@ private void prepareCoverThis()
@Test
void testGetDeepCoverageStatus_MissingCoverage()
{
when(this.itemMock.getNeedsArtifactTypes()).thenReturn(Arrays.asList(DSN));
when(this.coveredItemMock.getNeedsArtifactTypes()).thenReturn(Arrays.asList(IMPL, UMAN));
this.linkedItem.addLinkToItemWithStatus(this.coveredLinkedItem, LinkStatus.COVERED_SHALLOW);
assertItemDeepCoverageStatus(this.linkedItem, DeepCoverageStatus.UNCOVERED);
Expand Down Expand Up @@ -247,7 +242,6 @@ void testGetTitleWithFallback_HasTitle()
{
final String expectedReturn = "title";
when(this.itemMock.getTitle()).thenReturn(expectedReturn);
when(this.itemMock.getId()).thenReturn(SpecificationItemId.parseId("foo~wrong-return~1"));
assertThat(this.linkedItem.getTitleWithFallback(), equalTo(expectedReturn));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package org.itsallcode.openfasttrace.core;

import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.lenient;

import org.itsallcode.openfasttrace.api.core.*;
import org.itsallcode.openfasttrace.core.LinkedItemIndex.SpecificationItemIdWithoutVersion;
Expand All @@ -13,13 +12,10 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import nl.jqno.equalsverifier.EqualsVerifier;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class TestLinkedItemIndex
{
private final static SpecificationItemId DUPLICATE_ID_1 = SpecificationItemId.createId("type",
Expand All @@ -38,10 +34,10 @@ class TestLinkedItemIndex
@BeforeEach
public void prepareTest()
{
when(this.duplicateIdItem1Mock.getId()).thenReturn(DUPLICATE_ID_1);
when(this.duplicateIdItem2Mock.getId()).thenReturn(DUPLICATE_ID_2);
when(this.uniqueIdItemMock.getId()).thenReturn(UNIQUE_ID);
when(this.duplicateIdIgnoringVersionItemMock.getId())
lenient().when(this.duplicateIdItem1Mock.getId()).thenReturn(DUPLICATE_ID_1);
lenient().when(this.duplicateIdItem2Mock.getId()).thenReturn(DUPLICATE_ID_2);
lenient().when(this.uniqueIdItemMock.getId()).thenReturn(UNIQUE_ID);
lenient().when(this.duplicateIdIgnoringVersionItemMock.getId())
.thenReturn(DUPLICATE_ID_INGORING_VERSION);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import static org.itsallcode.openfasttrace.core.SpecificationItemAssertions.*;
import static org.itsallcode.openfasttrace.testutil.core.SampleArtifactTypes.*;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;

import java.util.Arrays;
import java.util.List;
Expand All @@ -19,12 +18,9 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

// [utest->dsn~linked-specification-item~1]
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class TestLinkedSpecificationItem
{
private LinkedSpecificationItem linkedItem;
Expand All @@ -40,10 +36,10 @@ public void prepareAllTests()
this.linkedItem = new LinkedSpecificationItem(this.itemMock);
this.coveredLinkedItem = new LinkedSpecificationItem(this.coveredItemMock);
this.otherLinkedItem = new LinkedSpecificationItem(this.otherItemMock);
when(this.itemMock.getStatus()).thenReturn(ItemStatus.APPROVED);
when(this.itemMock.getId()).thenReturn(SpecificationItemId.parseId("a~item~1"));
when(this.coveredItemMock.getId()).thenReturn(SpecificationItemId.parseId("a~covered~1"));
when(this.otherItemMock.getId()).thenReturn(SpecificationItemId.parseId("a~other~1"));
lenient().when(this.itemMock.getStatus()).thenReturn(ItemStatus.APPROVED);
lenient().when(this.itemMock.getId()).thenReturn(SpecificationItemId.parseId("a~item~1"));
lenient().when(this.coveredItemMock.getId()).thenReturn(SpecificationItemId.parseId("a~covered~1"));
lenient().when(this.otherItemMock.getId()).thenReturn(SpecificationItemId.parseId("a~other~1"));
}

@Test
Expand Down Expand Up @@ -129,7 +125,7 @@ void testGetDeepCoverageStatus_Covered()
private void prepareCoverThis()
{
when(this.itemMock.getNeedsArtifactTypes()).thenReturn(Arrays.asList(IMPL));
when(this.itemMock.getArtifactType()).thenReturn(DSN);
lenient().when(this.itemMock.getArtifactType()).thenReturn(DSN);
when(this.coveredItemMock.getArtifactType()).thenReturn(IMPL);
this.linkedItem.addLinkToItemWithStatus(this.coveredLinkedItem, LinkStatus.COVERED_SHALLOW);
}
Expand All @@ -138,7 +134,6 @@ private void prepareCoverThis()
@Test
void testGetDeepCoverageStatus_MissingCoverage()
{
when(this.itemMock.getNeedsArtifactTypes()).thenReturn(Arrays.asList(DSN));
when(this.coveredItemMock.getNeedsArtifactTypes()).thenReturn(Arrays.asList(IMPL, UMAN));
this.linkedItem.addLinkToItemWithStatus(this.coveredLinkedItem, LinkStatus.COVERED_SHALLOW);
assertItemDeepCoverageStatus(this.linkedItem, DeepCoverageStatus.UNCOVERED);
Expand Down Expand Up @@ -249,7 +244,6 @@ void testGetTitleWithFallback_HasTitle()
{
final String expectedReturn = "title";
when(this.itemMock.getTitle()).thenReturn(expectedReturn);
when(this.itemMock.getId()).thenReturn(SpecificationItemId.parseId("foo~wrong-return~1"));
assertThat(this.linkedItem.getTitleWithFallback(), equalTo(expectedReturn));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package org.itsallcode.openfasttrace.core;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.sameInstance;
import static org.hamcrest.Matchers.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.same;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -17,9 +15,7 @@

import org.itsallcode.openfasttrace.api.ReportSettings;
import org.itsallcode.openfasttrace.api.core.*;
import org.itsallcode.openfasttrace.api.importer.ImportSettings;
import org.itsallcode.openfasttrace.api.importer.ImporterService;
import org.itsallcode.openfasttrace.api.importer.MultiFileImporter;
import org.itsallcode.openfasttrace.api.importer.*;
import org.itsallcode.openfasttrace.core.exporter.ExporterService;
import org.itsallcode.openfasttrace.core.report.ReportService;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -28,11 +24,8 @@
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class TestOftRunner
{
private static final Path PATH = Paths.get("myPath");
Expand Down Expand Up @@ -66,20 +59,20 @@ void setUp()

oftRunner = new OftRunner(serviceFactoryMock);

when(serviceFactoryMock.createImporterService(any())).thenReturn(importerServiceMock);
when(importerServiceMock.createImporter()).thenReturn(multiFileImporterMock);
when(multiFileImporterMock.importAny(any())).thenReturn(multiFileImporterMock);
when(multiFileImporterMock.getImportedItems()).thenReturn(importedItems);
lenient().when(serviceFactoryMock.createImporterService(any())).thenReturn(importerServiceMock);
lenient().when(importerServiceMock.createImporter()).thenReturn(multiFileImporterMock);
lenient().when(multiFileImporterMock.importAny(any())).thenReturn(multiFileImporterMock);
lenient().when(multiFileImporterMock.getImportedItems()).thenReturn(importedItems);

when(serviceFactoryMock.createLinker(same(importedItems))).thenReturn(linkerMock);
when(linkerMock.link()).thenReturn(linkedItems);
lenient().when(serviceFactoryMock.createLinker(same(importedItems))).thenReturn(linkerMock);
lenient().when(linkerMock.link()).thenReturn(linkedItems);

when(serviceFactoryMock.createTracer()).thenReturn(tracerMock);
when(tracerMock.trace(same(linkedItems))).thenReturn(traceMock);
lenient().when(serviceFactoryMock.createTracer()).thenReturn(tracerMock);
lenient().when(tracerMock.trace(same(linkedItems))).thenReturn(traceMock);

when(serviceFactoryMock.createExporterService()).thenReturn(exporterServiceMock);
lenient().when(serviceFactoryMock.createExporterService()).thenReturn(exporterServiceMock);

when(serviceFactoryMock.createReportService(any())).thenReturn(reportServiceMock);
lenient().when(serviceFactoryMock.createReportService(any())).thenReturn(reportServiceMock);
}

@Test
Expand All @@ -92,7 +85,7 @@ void testImportItemsWithDefaultImportSettings()
assertDefaultSettings(arg.getValue());
}

private void assertDefaultSettings(ImportSettings actual)
private void assertDefaultSettings(final ImportSettings actual)
{
assertThat(actual.getFilters().isAnyCriteriaSet(), is(false));
assertThat(actual.getFilters().isArtifactTypeCriteriaSet(), is(false));
Expand Down Expand Up @@ -129,7 +122,7 @@ void testExportToPathListOfSpecificationItemPath()
assertDefaultExportSettings(arg.getValue());
}

private void assertDefaultExportSettings(ExportSettings settings)
private void assertDefaultExportSettings(final ExportSettings settings)
{
assertThat(settings.getNewline(), equalTo(Newline.UNIX));
assertThat(settings.getOutputFormat(), equalTo("specobject"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.when;

import java.util.Arrays;

import org.itsallcode.openfasttrace.api.core.LinkedSpecificationItem;
import org.itsallcode.openfasttrace.api.core.SpecificationItemId;
import org.itsallcode.openfasttrace.api.core.Trace;
import org.itsallcode.openfasttrace.api.core.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class TestTracer
{
private static final SpecificationItemId ID_A = SpecificationItemId.parseId("req~a~1");
Expand All @@ -34,9 +30,9 @@ class TestTracer
@BeforeEach
public void prepareTest()
{
when(this.aMock.getId()).thenReturn(ID_A);
when(this.bMock.getId()).thenReturn(ID_B);
when(this.cMock.getId()).thenReturn(ID_C);
lenient().when(this.aMock.getId()).thenReturn(ID_A);
lenient().when(this.bMock.getId()).thenReturn(ID_B);
lenient().when(this.cMock.getId()).thenReturn(ID_C);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@
import org.itsallcode.junit.sysextensions.AssertExit;
import org.itsallcode.junit.sysextensions.ExitGuard;
import org.itsallcode.openfasttrace.api.exporter.ExporterException;
import org.itsallcode.openfasttrace.testutil.TestAssumptions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(ExitGuard.class)
class TestCliStarter
{
@BeforeAll
static void assumeSecurityManagerSupported()
{
TestAssumptions.assumeSecurityManagerSupported();
}

@Test
void testRunWithoutArguments()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.same;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.when;

import java.nio.file.Paths;
Expand All @@ -19,14 +20,11 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

/**
* Test for {@link ImporterFactoryLoader}
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class TestImporterFactoryLoader
{
@Mock
Expand All @@ -49,9 +47,9 @@ void beforeEach()
this.loader = new ImporterFactoryLoader(this.serviceLoaderMock);
this.file = RealFileInput.forPath(Paths.get("dir", "name"));

when(this.supportedFactory1.supportsFile(same(this.file))).thenReturn(true);
when(this.supportedFactory2.supportsFile(same(this.file))).thenReturn(true);
when(this.unsupportedFactory.supportsFile(same(this.file))).thenReturn(false);
lenient().when(this.supportedFactory1.supportsFile(same(this.file))).thenReturn(true);
lenient().when(this.supportedFactory2.supportsFile(same(this.file))).thenReturn(true);
lenient().when(this.unsupportedFactory.supportsFile(same(this.file))).thenReturn(false);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
import static org.hamcrest.Matchers.sameInstance;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.same;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.*;

import org.itsallcode.openfasttrace.api.core.SpecificationItem;
import org.itsallcode.openfasttrace.api.importer.*;
Expand All @@ -24,11 +20,8 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class TestMultiFileImporter
{
private static final Path FOLDER = Paths.get("src/test/resources/markdown");
Expand All @@ -53,9 +46,10 @@ void beforeEach()
this.multiFileImporter = new MultiFileImporterImpl(this.specItemBuilderMock,
this.factoryLoaderMock);

when(this.factoryLoaderMock.supportsFile(any())).thenReturn(true);
when(this.factoryLoaderMock.getImporterFactory(any())).thenReturn(Optional.of(this.importerFactoryMock));
when(this.importerFactoryMock.createImporter(any(), same(this.specItemBuilderMock)))
lenient().when(this.factoryLoaderMock.supportsFile(any())).thenReturn(true);
lenient().when(this.factoryLoaderMock.getImporterFactory(any()))
.thenReturn(Optional.of(this.importerFactoryMock));
lenient().when(this.importerFactoryMock.createImporter(any(), same(this.specItemBuilderMock)))
.thenReturn(this.importerMock);
}

Expand Down
Loading

0 comments on commit 6466a4b

Please sign in to comment.