This is an automated email from the ASF dual-hosted git repository. royteeuwen pushed a commit to branch bugfix/SLING-13134 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-committer-cli.git
commit d4d0fa598f91ce131cb53643e0bb75498cd01ff6 Author: Roy Teeuwen <[email protected]> AuthorDate: Mon Mar 9 22:05:14 2026 +0100 Update project to compile on Java 24 --- pom.xml | 14 +-- .../apache/sling/cli/impl/junit/LogCapture.java | 87 ++++++++++++++ .../impl/release/PrepareVoteEmailCommandTest.java | 10 +- .../cli/impl/release/TallyVotesCommandTest.java | 126 +++++++++------------ .../impl/release/UpdateReporterCommandTest.java | 102 +++++++---------- 5 files changed, 191 insertions(+), 148 deletions(-) diff --git a/pom.xml b/pom.xml index 4ca619a..9f6517c 100644 --- a/pom.xml +++ b/pom.xml @@ -144,19 +144,19 @@ <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> - <version>2.25.1</version> + <version>5.22.0</version> <scope>test</scope> </dependency> <dependency> - <groupId>org.powermock</groupId> - <artifactId>powermock-api-mockito2</artifactId> - <version>2.0.2</version> + <groupId>org.apache.commons</groupId> + <artifactId>commons-lang3</artifactId> + <version>3.14.0</version> <scope>test</scope> </dependency> <dependency> - <groupId>org.powermock</groupId> - <artifactId>powermock-module-junit4</artifactId> - <version>2.0.2</version> + <groupId>ch.qos.logback</groupId> + <artifactId>logback-classic</artifactId> + <version>1.5.6</version> <scope>test</scope> </dependency> <dependency> diff --git a/src/test/java/org/apache/sling/cli/impl/junit/LogCapture.java b/src/test/java/org/apache/sling/cli/impl/junit/LogCapture.java new file mode 100644 index 0000000..95a6cdb --- /dev/null +++ b/src/test/java/org/apache/sling/cli/impl/junit/LogCapture.java @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.sling.cli.impl.junit; + +import java.util.List; +import java.util.stream.Collectors; + +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; +import org.junit.rules.ExternalResource; +import org.slf4j.LoggerFactory; + +/** + * JUnit rule that captures log output for a given class using Logback's {@link ListAppender}. + * Usage: + * <pre> + * @Rule + * public LogCapture logCapture = new LogCapture(MyClass.class); + * + * @Test + * public void test() { + * // ... exercise code ... + * assertTrue(logCapture.containsMessage("expected output")); + * } + * </pre> + */ +public class LogCapture extends ExternalResource { + + private final Class<?>[] loggerClasses; + private ListAppender<ILoggingEvent> appender; + + public LogCapture(Class<?>... loggerClasses) { + this.loggerClasses = loggerClasses; + } + + @Override + protected void before() { + appender = new ListAppender<>(); + appender.start(); + for (Class<?> clazz : loggerClasses) { + Logger logger = (Logger) LoggerFactory.getLogger(clazz); + logger.addAppender(appender); + } + } + + @Override + protected void after() { + for (Class<?> clazz : loggerClasses) { + Logger logger = (Logger) LoggerFactory.getLogger(clazz); + logger.detachAppender(appender); + } + appender.stop(); + } + + public List<String> getMessages() { + return appender.list.stream().map(ILoggingEvent::getFormattedMessage).collect(Collectors.toList()); + } + + public boolean containsMessage(String substring) { + return appender.list.stream().anyMatch(e -> e.getFormattedMessage().contains(substring)); + } + + public String messageAt(int index) { + return appender.list.get(index).getFormattedMessage(); + } + + public int size() { + return appender.list.size(); + } +} diff --git a/src/test/java/org/apache/sling/cli/impl/release/PrepareVoteEmailCommandTest.java b/src/test/java/org/apache/sling/cli/impl/release/PrepareVoteEmailCommandTest.java index cc94b6e..2b4f1be 100644 --- a/src/test/java/org/apache/sling/cli/impl/release/PrepareVoteEmailCommandTest.java +++ b/src/test/java/org/apache/sling/cli/impl/release/PrepareVoteEmailCommandTest.java @@ -23,6 +23,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Set; +import org.apache.commons.lang3.reflect.FieldUtils; import org.apache.sling.cli.impl.Command; import org.apache.sling.cli.impl.DateProvider; import org.apache.sling.cli.impl.ExecutionMode; @@ -38,7 +39,6 @@ import org.apache.sling.testing.mock.osgi.junit.OsgiContext; import org.junit.Rule; import org.junit.Test; import org.osgi.framework.ServiceReference; -import org.powermock.reflect.Whitebox; import picocli.CommandLine; import static org.junit.Assert.assertEquals; @@ -62,10 +62,10 @@ public class PrepareVoteEmailCommandTest { CommandLine commandLine = mock(CommandLine.class); when(commandSpec.commandLine()).thenReturn(commandLine); when(commandLine.isUsageHelpRequested()).thenReturn(false); - Whitebox.setInternalState(prepareVoteEmailCommand, "spec", commandSpec); - Whitebox.setInternalState(reusableCLIOptions, "executionMode", ExecutionMode.AUTO); - Whitebox.setInternalState(prepareVoteEmailCommand, "reusableCLIOptions", reusableCLIOptions); - Whitebox.setInternalState(prepareVoteEmailCommand, "repositoryId", 123); + FieldUtils.writeField(prepareVoteEmailCommand, "spec", commandSpec, true); + FieldUtils.writeField(reusableCLIOptions, "executionMode", ExecutionMode.AUTO, true); + FieldUtils.writeField(prepareVoteEmailCommand, "reusableCLIOptions", reusableCLIOptions, true); + FieldUtils.writeField(prepareVoteEmailCommand, "repositoryId", 123, true); osgiContext.registerInjectActivateService(prepareVoteEmailCommand); ServiceReference<?> reference = osgiContext.bundleContext().getServiceReference(Command.class.getName()); diff --git a/src/test/java/org/apache/sling/cli/impl/release/TallyVotesCommandTest.java b/src/test/java/org/apache/sling/cli/impl/release/TallyVotesCommandTest.java index 8ef0b22..96b803d 100644 --- a/src/test/java/org/apache/sling/cli/impl/release/TallyVotesCommandTest.java +++ b/src/test/java/org/apache/sling/cli/impl/release/TallyVotesCommandTest.java @@ -26,11 +26,13 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import org.apache.commons.lang3.reflect.FieldUtils; import org.apache.sling.cli.impl.Command; import org.apache.sling.cli.impl.Credentials; import org.apache.sling.cli.impl.CredentialsService; import org.apache.sling.cli.impl.DateProvider; import org.apache.sling.cli.impl.ExecutionMode; +import org.apache.sling.cli.impl.junit.LogCapture; import org.apache.sling.cli.impl.mail.Email; import org.apache.sling.cli.impl.mail.Mailer; import org.apache.sling.cli.impl.mail.VoteThreadFinder; @@ -42,31 +44,17 @@ import org.apache.sling.testing.mock.osgi.junit.OsgiContext; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.runner.RunWith; import org.osgi.framework.ServiceReference; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import picocli.CommandLine; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; -import static org.powermock.api.mockito.PowerMockito.mockStatic; - -@RunWith(PowerMockRunner.class) -@PowerMockIgnore({ - // https://github.com/powermock/powermock/issues/864 - "com.sun.org.apache.xerces.*", - "javax.xml.*", - "org.w3c.dom.*", - "javax.net.ssl.*" -}) + public class TallyVotesCommandTest { @Before @@ -79,12 +67,12 @@ public class TallyVotesCommandTest { @Rule public final OsgiContext osgiContext = new OsgiContext(); + @Rule + public final LogCapture logCapture = new LogCapture(TallyVotesCommand.class); + @Test - @PrepareForTest({LoggerFactory.class}) public void testDryRun() throws Exception { - mockStatic(LoggerFactory.class); - Logger logger = mock(Logger.class); - when(LoggerFactory.getLogger(TallyVotesCommand.class)).thenReturn(logger); + Mailer mailer = mock(Mailer.class); List<Email> thread = new ArrayList<>() { { add(mockEmail("[email protected]", "John Doe")); @@ -96,42 +84,35 @@ public class TallyVotesCommandTest { add(mockEmail("[email protected]", "Jörg Hoh")); } }; - prepareExecution(mock(Mailer.class), thread); - TallyVotesCommand tallyVotesCommand = spy(new TallyVotesCommand()); - ReusableCLIOptions reusableCLIOptions = mock(ReusableCLIOptions.class); - Whitebox.setInternalState(reusableCLIOptions, "executionMode", ExecutionMode.DRY_RUN); - Whitebox.setInternalState(tallyVotesCommand, "repositoryId", 123); - Whitebox.setInternalState(tallyVotesCommand, "reusableCLIOptions", reusableCLIOptions); - osgiContext.registerInjectActivateService(tallyVotesCommand); - ServiceReference<?> reference = osgiContext.bundleContext().getServiceReference(Command.class.getName()); - Command command = (Command) osgiContext.bundleContext().getService(reference); + prepareExecution(mailer, thread); + Command command = createCommand(123, ExecutionMode.DRY_RUN); assertEquals(CommandLine.ExitCode.OK, (int) command.call()); - verify(logger) - .info("From: John Doe <[email protected]>\n" + "To: \"Sling Developers List\" <[email protected]>\n" - + "Reply-To: \"Sling Developers List\" <[email protected]>\n" - + "Date: Thu, 1 Jan 1970 01:00:00 +0100\n" - + "Subject: [RESULT] [VOTE] Release Apache Sling CLI Test 1.0.0\n" - + "\n" - + "Hi,\n" - + "\n" - + "The vote has passed with the following result:\n" - + "\n" - + "+1 (binding): Alice, Bob, Charlie, John Doe, Joerg Hoh\n" - + "+1 (non-binding): Daniel\n" - + "\n" - + "I will copy this release to the Sling dist directory and\n" - + "promote the artifacts to the central Maven repository.\n" - + "\n" - + "Regards,\n" - + "John Doe\n"); + verifyNoInteractions(mailer); + assertTrue(logCapture.containsMessage( + "The following email would be sent from your @apache.org address (see the \"From:\" header):")); + assertTrue(logCapture.containsMessage("From: John Doe <[email protected]>\n" + + "To: \"Sling Developers List\" <[email protected]>\n" + + "Reply-To: \"Sling Developers List\" <[email protected]>\n" + + "Date: Thu, 1 Jan 1970 01:00:00 +0100\n" + + "Subject: [RESULT] [VOTE] Release Apache Sling CLI Test 1.0.0\n" + + "\n" + + "Hi,\n" + + "\n" + + "The vote has passed with the following result:\n" + + "\n" + + "+1 (binding): Alice, Bob, Charlie, John Doe, Joerg Hoh\n" + + "+1 (non-binding): Daniel\n" + + "\n" + + "I will copy this release to the Sling dist directory and\n" + + "promote the artifacts to the central Maven repository.\n" + + "\n" + + "Regards,\n" + + "John Doe\n")); } @Test - @PrepareForTest({LoggerFactory.class}) public void testDryRunNotEnoughBindingVotes() throws Exception { - mockStatic(LoggerFactory.class); - Logger logger = mock(Logger.class); - when(LoggerFactory.getLogger(TallyVotesCommand.class)).thenReturn(logger); + Mailer mailer = mock(Mailer.class); List<Email> thread = new ArrayList<>() { { add(mockEmail("[email protected]", "John Doe")); @@ -140,17 +121,12 @@ public class TallyVotesCommandTest { add(mockEmail("[email protected]", "Daniel")); } }; - prepareExecution(mock(Mailer.class), thread); - TallyVotesCommand tallyVotesCommand = spy(new TallyVotesCommand()); - ReusableCLIOptions reusableCLIOptions = mock(ReusableCLIOptions.class); - Whitebox.setInternalState(reusableCLIOptions, "executionMode", ExecutionMode.DRY_RUN); - Whitebox.setInternalState(tallyVotesCommand, "repositoryId", 123); - Whitebox.setInternalState(tallyVotesCommand, "reusableCLIOptions", reusableCLIOptions); - osgiContext.registerInjectActivateService(tallyVotesCommand); - ServiceReference<?> reference = osgiContext.bundleContext().getServiceReference(Command.class.getName()); - Command command = (Command) osgiContext.bundleContext().getService(reference); + prepareExecution(mailer, thread); + Command command = createCommand(123, ExecutionMode.DRY_RUN); assertEquals(CommandLine.ExitCode.USAGE, (int) command.call()); - verify(logger).info("Release {} does not have at least 3 binding votes.", "Apache Sling CLI Test 1.0.0"); + verifyNoInteractions(mailer); + assertTrue(logCapture.containsMessage( + "Release Apache Sling CLI Test 1.0.0 does not have at least 3 binding votes.")); } @Test @@ -167,14 +143,7 @@ public class TallyVotesCommandTest { }; Mailer mailer = mock(Mailer.class); prepareExecution(mailer, thread); - TallyVotesCommand tallyVotesCommand = spy(new TallyVotesCommand()); - ReusableCLIOptions reusableCLIOptions = mock(ReusableCLIOptions.class); - Whitebox.setInternalState(reusableCLIOptions, "executionMode", ExecutionMode.AUTO); - Whitebox.setInternalState(tallyVotesCommand, "repositoryId", 123); - Whitebox.setInternalState(tallyVotesCommand, "reusableCLIOptions", reusableCLIOptions); - osgiContext.registerInjectActivateService(tallyVotesCommand); - ServiceReference<?> reference = osgiContext.bundleContext().getServiceReference(Command.class.getName()); - Command command = (Command) osgiContext.bundleContext().getService(reference); + Command command = createCommand(123, ExecutionMode.AUTO); assertEquals(CommandLine.ExitCode.OK, (int) command.call()); verify(mailer) .send("From: John Doe <[email protected]>\n" + "To: \"Sling Developers List\" <[email protected]>\n" @@ -196,6 +165,17 @@ public class TallyVotesCommandTest { + "John Doe\n"); } + private Command createCommand(int repositoryId, ExecutionMode executionMode) throws IllegalAccessException { + TallyVotesCommand tallyVotesCommand = spy(new TallyVotesCommand()); + ReusableCLIOptions reusableCLIOptions = mock(ReusableCLIOptions.class); + FieldUtils.writeField(reusableCLIOptions, "executionMode", executionMode, true); + FieldUtils.writeField(tallyVotesCommand, "repositoryId", repositoryId, true); + FieldUtils.writeField(tallyVotesCommand, "reusableCLIOptions", reusableCLIOptions, true); + osgiContext.registerInjectActivateService(tallyVotesCommand); + ServiceReference<?> reference = osgiContext.bundleContext().getServiceReference(Command.class.getName()); + return (Command) osgiContext.bundleContext().getService(reference); + } + private Email mockEmail(String address, String name) throws Exception { Email email = mock(Email.class); when(email.getBody()).thenReturn("+1"); @@ -203,7 +183,7 @@ public class TallyVotesCommandTest { return email; } - private void prepareExecution(Mailer mailer, List<Email> thread) throws IOException { + private void prepareExecution(Mailer mailer, List<Email> thread) throws IOException, IllegalAccessException { CredentialsService credentialsService = mock(CredentialsService.class); when(credentialsService.getAsfCredentials()).thenReturn(new Credentials("johndoe", "secret")); @@ -218,8 +198,8 @@ public class TallyVotesCommandTest { add(new Member("joerghoh", "Joerg Hoh", true)); } }; - Whitebox.setInternalState(membersFinder, "members", members); - Whitebox.setInternalState(membersFinder, "lastCheck", System.currentTimeMillis()); + FieldUtils.writeField(membersFinder, "members", members, true); + FieldUtils.writeField(membersFinder, "lastCheck", System.currentTimeMillis(), true); StagingRepository stagingRepository = mock(StagingRepository.class); when(stagingRepository.getDescription()).thenReturn("Apache Sling CLI Test 1.0.0"); diff --git a/src/test/java/org/apache/sling/cli/impl/release/UpdateReporterCommandTest.java b/src/test/java/org/apache/sling/cli/impl/release/UpdateReporterCommandTest.java index 430bfd0..10f53fa 100644 --- a/src/test/java/org/apache/sling/cli/impl/release/UpdateReporterCommandTest.java +++ b/src/test/java/org/apache/sling/cli/impl/release/UpdateReporterCommandTest.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.util.HashSet; import java.util.List; +import org.apache.commons.lang3.reflect.FieldUtils; import org.apache.http.StatusLine; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.impl.client.CloseableHttpClient; @@ -30,38 +31,26 @@ import org.apache.sling.cli.impl.ExecutionMode; import org.apache.sling.cli.impl.InputOption; import org.apache.sling.cli.impl.UserInput; import org.apache.sling.cli.impl.http.HttpClientFactory; +import org.apache.sling.cli.impl.junit.LogCapture; import org.apache.sling.cli.impl.nexus.RepositoryService; import org.apache.sling.cli.impl.nexus.StagingRepository; import org.apache.sling.testing.mock.osgi.junit.OsgiContext; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.mockito.MockedStatic; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; -import static org.powermock.api.mockito.PowerMockito.mockStatic; -@RunWith(PowerMockRunner.class) -@PowerMockIgnore({ - // https://github.com/powermock/powermock/issues/864 - "com.sun.org.apache.xerces.*", - "javax.xml.*", - "org.w3c.dom.*" -}) public class UpdateReporterCommandTest { private CloseableHttpClient client; @@ -69,6 +58,9 @@ public class UpdateReporterCommandTest { @Rule public OsgiContext osgiContext = new OsgiContext(); + @Rule + public final LogCapture logCapture = new LogCapture(UpdateReporterCommand.class); + @Before public void before() throws IOException { RepositoryService repositoryService = mock(RepositoryService.class); @@ -87,47 +79,38 @@ public class UpdateReporterCommandTest { } @Test - @PrepareForTest({LoggerFactory.class}) public void testDryRun() throws Exception { - mockStatic(LoggerFactory.class); - Logger logger = mock(Logger.class); - when(LoggerFactory.getLogger(UpdateReporterCommand.class)).thenReturn(logger); - UpdateReporterCommand updateReporterCommand = spy(new UpdateReporterCommand()); - Whitebox.setInternalState(updateReporterCommand, "repositoryId", 42); - ReusableCLIOptions reusableCLIOptions = mock(ReusableCLIOptions.class); - Whitebox.setInternalState(reusableCLIOptions, "executionMode", ExecutionMode.DRY_RUN); - Whitebox.setInternalState(updateReporterCommand, "reusableCLIOptions", reusableCLIOptions); - osgiContext.registerInjectActivateService(updateReporterCommand); - Command updateReporter = osgiContext.getService(Command.class); - assertTrue( - "Expected to retrieve the UpdateReporterCommand from the mocked OSGi environment.", - updateReporter instanceof UpdateReporterCommand); + Command updateReporter = createCommand(42, ExecutionMode.DRY_RUN); assertEquals(0, (int) updateReporter.call()); - verify(logger).info("The following {} would be added to the Apache Reporter System:", "releases"); - verify(logger).info(" - {}", "Apache Sling CLI 1"); - verify(logger).info(" - {}", "Apache Sling CLI 2"); - verifyNoMoreInteractions(logger); + verifyNoInteractions(client); + assertEquals(3, logCapture.size()); + assertTrue(logCapture.containsMessage("The following releases would be added to the Apache Reporter System:")); + assertTrue(logCapture.containsMessage(" - Apache Sling CLI 1")); + assertTrue(logCapture.containsMessage(" - Apache Sling CLI 2")); } @Test - @PrepareForTest({UserInput.class}) public void testInteractive() throws Exception { - UpdateReporterCommand updateReporterCommand = spy(new UpdateReporterCommand()); - Whitebox.setInternalState(updateReporterCommand, "repositoryId", 42); - ReusableCLIOptions reusableCLIOptions = mock(ReusableCLIOptions.class); - Whitebox.setInternalState(reusableCLIOptions, "executionMode", ExecutionMode.INTERACTIVE); - Whitebox.setInternalState(updateReporterCommand, "reusableCLIOptions", reusableCLIOptions); - osgiContext.registerInjectActivateService(updateReporterCommand); - Command updateReporter = osgiContext.getService(Command.class); - assertTrue( - "Expected to retrieve the UpdateReporterCommand from the mocked OSGi environment.", - updateReporter instanceof UpdateReporterCommand); + try (MockedStatic<UserInput> userInputMock = mockStatic(UserInput.class)) { + Command updateReporter = createCommand(42, ExecutionMode.INTERACTIVE); + CloseableHttpResponse response = mock(CloseableHttpResponse.class); + StatusLine statusLine = mock(StatusLine.class); + String question = + "Should the following releases be added to the Apache Reporter System?\n - Apache Sling CLI 1\n - Apache Sling CLI 2\n"; + userInputMock.when(() -> UserInput.yesNo(question, InputOption.YES)).thenReturn(InputOption.YES); + when(response.getStatusLine()).thenReturn(statusLine); + when(statusLine.getStatusCode()).thenReturn(200); + when(client.execute(any())).thenReturn(response); + assertEquals(0, (int) updateReporter.call()); + verify(client, times(2)).execute(any()); + } + } + + @Test + public void testAuto() throws Exception { + Command updateReporter = createCommand(42, ExecutionMode.AUTO); CloseableHttpResponse response = mock(CloseableHttpResponse.class); StatusLine statusLine = mock(StatusLine.class); - mockStatic(UserInput.class); - String question = - "Should the following releases be added to the Apache Reporter System?\n - Apache Sling CLI 1\n - Apache Sling CLI 2\n"; - when(UserInput.yesNo(question, InputOption.YES)).thenReturn(InputOption.YES); when(response.getStatusLine()).thenReturn(statusLine); when(statusLine.getStatusCode()).thenReturn(200); when(client.execute(any())).thenReturn(response); @@ -135,24 +118,17 @@ public class UpdateReporterCommandTest { verify(client, times(2)).execute(any()); } - @Test - public void testAuto() throws Exception { + private Command createCommand(int repositoryId, ExecutionMode executionMode) throws IllegalAccessException { UpdateReporterCommand updateReporterCommand = spy(new UpdateReporterCommand()); - Whitebox.setInternalState(updateReporterCommand, "repositoryId", 42); + FieldUtils.writeField(updateReporterCommand, "repositoryId", repositoryId, true); ReusableCLIOptions reusableCLIOptions = mock(ReusableCLIOptions.class); - Whitebox.setInternalState(reusableCLIOptions, "executionMode", ExecutionMode.AUTO); - Whitebox.setInternalState(updateReporterCommand, "reusableCLIOptions", reusableCLIOptions); + FieldUtils.writeField(reusableCLIOptions, "executionMode", executionMode, true); + FieldUtils.writeField(updateReporterCommand, "reusableCLIOptions", reusableCLIOptions, true); osgiContext.registerInjectActivateService(updateReporterCommand); - Command updateReporter = osgiContext.getService(Command.class); + Command result = osgiContext.getService(Command.class); assertTrue( "Expected to retrieve the UpdateReporterCommand from the mocked OSGi environment.", - updateReporter instanceof UpdateReporterCommand); - CloseableHttpResponse response = mock(CloseableHttpResponse.class); - StatusLine statusLine = mock(StatusLine.class); - when(response.getStatusLine()).thenReturn(statusLine); - when(statusLine.getStatusCode()).thenReturn(200); - when(client.execute(any())).thenReturn(response); - assertEquals(0, (int) updateReporter.call()); - verify(client, times(2)).execute(any()); + result instanceof UpdateReporterCommand); + return result; } }
