Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add lots of mocked tests to sop-java-picocli
- Loading branch information
1 parent
f537868
commit 30e70c8
Showing
15 changed files
with
732 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
sop-java-picocli/src/test/java/sop/cli/picocli/ArmorCmdTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* Copyright 2021 Paul Schaub. | ||
* | ||
* Licensed 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 sop.cli.picocli; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
import com.ginsberg.junit.exit.ExpectSystemExitWithStatus; | ||
import com.ginsberg.junit.exit.FailOnSystemExit; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import sop.Ready; | ||
import sop.SOP; | ||
import sop.enums.ArmorLabel; | ||
import sop.exception.SOPGPException; | ||
import sop.operation.Armor; | ||
|
||
public class ArmorCmdTest { | ||
|
||
private Armor armor; | ||
private SOP sop; | ||
|
||
@BeforeEach | ||
public void mockComponents() throws SOPGPException.BadData { | ||
armor = mock(Armor.class); | ||
sop = mock(SOP.class); | ||
when(sop.armor()).thenReturn(armor); | ||
when(armor.data(any())).thenReturn(nopReady()); | ||
|
||
SopCLI.setSopInstance(sop); | ||
} | ||
|
||
@Test | ||
public void assertAllowNestedIsCalledWhenFlagged() throws SOPGPException.UnsupportedOption { | ||
SopCLI.main(new String[] {"armor", "--allow-nested"}); | ||
verify(armor, times(1)).allowNested(); | ||
} | ||
|
||
@Test | ||
public void assertAllowNestedIsNotCalledByDefault() throws SOPGPException.UnsupportedOption { | ||
SopCLI.main(new String[] {"armor"}); | ||
verify(armor, never()).allowNested(); | ||
} | ||
|
||
@Test | ||
public void assertLabelIsNotCalledByDefault() throws SOPGPException.UnsupportedOption { | ||
SopCLI.main(new String[] {"armor"}); | ||
verify(armor, never()).label(any()); | ||
} | ||
|
||
@Test | ||
public void assertLabelIsCalledWhenFlaggedWithArgument() throws SOPGPException.UnsupportedOption { | ||
for (ArmorLabel label : ArmorLabel.values()) { | ||
SopCLI.main(new String[] {"armor", "--label", label.name()}); | ||
verify(armor, times(1)).label(label); | ||
} | ||
} | ||
|
||
@Test | ||
public void assertDataIsAlwaysCalled() throws SOPGPException.BadData { | ||
SopCLI.main(new String[] {"armor"}); | ||
verify(armor, times(1)).data(any()); | ||
} | ||
|
||
@Test | ||
@ExpectSystemExitWithStatus(37) | ||
public void assertThrowsForInvalidLabel() { | ||
SopCLI.main(new String[] {"armor", "--label", "Invalid"}); | ||
} | ||
|
||
@Test | ||
@ExpectSystemExitWithStatus(37) | ||
public void ifLabelsUnsupportedExit37() throws SOPGPException.UnsupportedOption { | ||
when(armor.label(any())).thenThrow(new SOPGPException.UnsupportedOption()); | ||
|
||
SopCLI.main(new String[] {"armor", "--label", "Sig"}); | ||
} | ||
|
||
@Test | ||
@ExpectSystemExitWithStatus(37) | ||
public void ifAllowNestedUnsupportedExit37() throws SOPGPException.UnsupportedOption { | ||
when(armor.allowNested()).thenThrow(new SOPGPException.UnsupportedOption()); | ||
|
||
SopCLI.main(new String[] {"armor", "--allow-nested"}); | ||
} | ||
|
||
@Test | ||
@ExpectSystemExitWithStatus(41) | ||
public void ifBadDataExit41() throws SOPGPException.BadData { | ||
when(armor.data(any())).thenThrow(new SOPGPException.BadData(new IOException())); | ||
|
||
SopCLI.main(new String[] {"armor"}); | ||
} | ||
|
||
@Test | ||
@FailOnSystemExit | ||
public void ifNoErrorsNoExit() { | ||
when(sop.armor()).thenReturn(armor); | ||
|
||
SopCLI.main(new String[] {"armor"}); | ||
} | ||
|
||
private static Ready nopReady() { | ||
return new Ready() { | ||
@Override | ||
public void writeTo(OutputStream outputStream) { | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.