From 9bd987dbbd379a5a58993ee1e0c1cc901574b1d0 Mon Sep 17 00:00:00 2001 From: Damian Szczepanik Date: Mon, 1 Jan 2024 10:07:16 +0100 Subject: [PATCH] Solves sonar: Only one method invocation is expected when testing runtime exceptions --- .github/workflows/sonarcloud.yml | 3 ++- .../java/pl/szczepanik/silencio/utils/IOUtilityTest.java | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml index 14273eb..83e6209 100644 --- a/.github/workflows/sonarcloud.yml +++ b/.github/workflows/sonarcloud.yml @@ -4,6 +4,7 @@ on: push: branches: - master + - package # do not validate pull requests because SONAR_TOKEN is available only for project owner jobs: @@ -21,7 +22,7 @@ jobs: distribution: adopt - name: Build with Maven - run: mvn --batch-mode package sonar:sonar -Dsonar.projectKey=damianszczepanik_silencio -Dsonar.organization=damianszczepanik -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=$SONAR_TOKEN + run: mvn --batch-mode package sonar:sonar -Dsonar.projectKey=damianszczepanik_silencio -Dsonar.organization=damianszczepanik -Dsonar.host.url=https://sonarcloud.io -Dsonar.token=$SONAR_TOKEN env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} diff --git a/src/test/java/pl/szczepanik/silencio/utils/IOUtilityTest.java b/src/test/java/pl/szczepanik/silencio/utils/IOUtilityTest.java index a43bb35..b5ff8f4 100644 --- a/src/test/java/pl/szczepanik/silencio/utils/IOUtilityTest.java +++ b/src/test/java/pl/szczepanik/silencio/utils/IOUtilityTest.java @@ -56,12 +56,15 @@ void shouldFailOnIOException() throws Exception { String errorMessage = "Something is wrong!"; + // given + URL url = new URL(URL_ADDRESS); + // when - when(IOUtils.toString(new URL(URL_ADDRESS), StandardCharsets.UTF_8)) + when(IOUtils.toString(url, StandardCharsets.UTF_8)) .thenThrow(new IOException(errorMessage)); // then - assertThatThrownBy(() -> IOUtility.urlToString(new URL(URL_ADDRESS))) + assertThatThrownBy(() -> IOUtility.urlToString(url)) .isInstanceOf(IntegrityException.class) .hasMessage(errorMessage); }