How to use Mockito to skip invoking a void method - java

I have a REST controller exposing an endpoint. When this endpoint is hit, a void method gets invoked and this method then goes off and pushes a file to a remote GitHub repo. The code is working beautifully.
My problem occurs when writing unit tests for the class. I don't want the actual void method to get invoked (because it's pushing a file to github). I've mocked the method to doNothing() when it's invoked, but the file is still being pushed for some reason. Where am i going wrong?
Below is my code:
//ApplicationController.java
#RestController
public class ApplicationController {
#Autowired
GitService gitService;
#GetMapping("/v1/whatevs")
public String push_policy() throws IOException, GitAPIException {
gitService.gitPush("Successfully pushed a fie to github...i think.");
return "pushed the file to github.";
}
}
//GitService.java
public interface GitService {
public void gitPush(String fileContents) throws IOException, GitAPIException;
}
//GitServiceImpl.java
#Component
public class GitServiceImpl implements GitService {
private static final Logger log = Logger.getLogger(GitServiceImpl.class.getName());
#Override
public void gitPush(String fileContents) throws IOException, GitAPIException {
// prepare a new folder for the cloned repository
File localPath = File.createTempFile(GIT_REPO, "");
if (!localPath.delete()) {
throw new IOException("Could not delete temporary file " + localPath);
}
// now clone repository
System.out.println("Cloning from" + REMOTE_GIT_URL + "to " + localPath);
try (Git result = Git.cloneRepository().setURI(REMOTE_GIT_URL).setDirectory(localPath)
.setCredentialsProvider(new UsernamePasswordCredentialsProvider(GIT_USER, GIT_PASSWORD)).call()) {
// Note: the call() returns an opened repository already which needs to be
// closed to avoid file handle leaks!
Repository repository = result.getRepository();
try (Git git = new Git(repository)) {
// create the file
Path path = Paths.get(String.format("%s/%s", localPath.getPath(), "someFileName"));
byte[] strToBytes = fileContents.getBytes();
Files.write(path, strToBytes);
// add the file to the repo
git.add().addFilepattern("someFileName").call();
// commit the changes
String commit_message = String
.format("[%s] Calico yaml file(s) generated by Astra Calico policy adaptor.", GIT_USER);
git.commit().setMessage(commit_message).call();
log.info("Committed file to repository at " + REMOTE_GIT_URL);
// push the commits
Iterable<PushResult> pushResults = git.push()
.setCredentialsProvider(new UsernamePasswordCredentialsProvider(GIT_USER, GIT_PASSWORD)).call();
pushResults.forEach(pushResult -> log.info(pushResult.getMessages()));
}
} finally {
// delete temp directory on disk
FileUtils.deleteDirectory(localPath);
}
}
}
My test. It's passing, but the gitService.gitpush() method I thought was being mocked, is pushing a file to github.
#RunWith(SpringRunner.class)
#SpringBootTest
#AutoConfigureMockMvc
public class ApplicationControllerTest {
#Autowired
private MockMvc mockMvc;
#Mock
GitService gitService;
//System under test
#InjectMocks
ApplicationController applicationController;
public void setup() {
mockMvc = MockMvcBuilders.standaloneSetup(applicationController).build();
}
#Test
public void controllerShouldReturnStatus200Ok() throws Exception {
Mockito.doNothing().when(gitService).gitPush(Mockito.anyString());
mockMvc.perform(
MockMvcRequestBuilders.get("/v1/whatevs")
).andExpect(MockMvcResultMatchers.status().isOk());
}
#Test
public void someTest() {
assertTrue(true);
}
}
How can I keep the .gitPush() method from being invoked at all? Am I simply mocking the service incorrectly?

Add #Before annotation to your setup method to
Add this to your before method MockitoAnnotations.initMocks(this).
It should work now

Related

Increase code coverage on method local objects or 3rd party library objects creation or 3rd party functions call

I have to unit test the below method, whereas all the lines of this code related to third party aws library. The method also returns nothing. So only test I can do is verifying the exception. Any other test can I do to improve the code coverage?
public void multipartUpload() throws InterruptedException {
TransferManager tm = TransferManagerBuilder.standard()
.withS3Client(s3Client)
.withMultipartUploadThreshold(1024l)
.build();
PutObjectRequest request = new PutObjectRequest(bucketName, keyName, filePath);
Upload upload = tm.upload(request);
upload.waitForCompletion();
}
Let see the code that needs to be tested:
public class DemoCodeCoverage {
public void showDemo(LibraryCode library) {
System.out.println("Hello World!");
library.runDemoApplication();
// Extract the below code to a method since LibraryCode is not passed
// Then ignore running that method
// LibraryCode library = new LibraryCode()
// library.runDemoApplication_1();
// library.runDemoApplication_2();
// library.runDemoApplication_3();
System.out.println("World ends here!");
}
public boolean showBranchingDemo(boolean signal) {
if (signal) {
signalShown();
} else {
noSignal();
}
return signal;
}
public void signalShown() {
System.out.println("signalShown!");
}
public void noSignal() {
System.out.println("NoSignal!");
}
}
public class LibraryCode {
// Library can be AWS/Database code which needs authentication
// And this authentication is not a concern for our UT
// Still will end up execption when we do our UT
public void runDemoApplication() {
throw new RuntimeException();
}
}
Below can give good code coverage:
public class DemoCodeCoverageTest {
#Test
public void testShowDemo() {
DemoCodeCoverage t = Mockito.spy(new DemoCodeCoverage());
LibraryCode lib = Mockito.mock(LibraryCode.class);
Mockito.doNothing().when(lib).runDemoApplication();
t.showDemo(lib);
// when(bloMock.doSomeStuff()).thenReturn(1);
// doReturn(1).when(bloMock).doSomeStuff();
}
#Test
public void testShowBranchingDemo() {
DemoCodeCoverage t = Mockito.spy(new DemoCodeCoverage());
assertEquals(true, t.showBranchingDemo(true));
assertEquals(false, t.showBranchingDemo(false));
}
#Test
public void testSignalShown() {
DemoCodeCoverage t = Mockito.spy(new DemoCodeCoverage());
t.showBranchingDemo(true);
Mockito.verify(t, times(1)).signalShown();
}
#Test
public void testNoSignal() {
DemoCodeCoverage t = Mockito.spy(new DemoCodeCoverage());
t.showBranchingDemo(false);
Mockito.verify(t, times(1)).noSignal();
}
}
Below are the steps to increase the test code coverage:
Case_1: Testing void method
Assume you have method the does not take any params and return nothing.
public void printHelloWorld() {
System.out.println("Hello World")
}
Still you can write test that calls this method and returns successfully without any runtimeException.
Actually we haven't tested anything here other than giving a option to run the code by our tests. Thus increase the code coverage.
Additionally you can verify the invocation:
Mockito.verify(instance, times(1)).printHelloWorld();
There are circumstances you cannot test those, example say it is third party library call, then the library might have tested already, we just need to run through it.
#Test
public void testPrintHelloWorld() {
// may be hibernate call/other 3rd party method call
instance.printHelloWorld();
}
If your tool is not strict for 100% code coverage, you can even ignore it and justify it.
Case_2: Testing a method with object created and called another method inside the testing method
Assume you have method the does call DB to add entry in Hello_World table also prints it in console like below.
public void printHelloWorld() throws DBException {
DBConnection db = new DBConnection();
db.createEntry(TABLE_NAME, "Hello World");
System.out.println("Hello World")
}
You can extract those db code into new method, then test it separately.
public void printHelloWorld() throws DBException {
makeHelloWorldEntryInTable();
System.out.println("Hello World")
}
public void makeHelloWorldEntryInTable() throws DBException {
DBConnection db = new DBConnection();
db.createEntry(TABLE_NAME, "Hello World");
}
While testing with DB you would expect the DBConnectionException as it is just unit test. So one test with #Test(expected=DBException) for makeHelloWorldEntryInTable, and another test on printHelloWorld() with skipping the method makeHelloWorldEntryInTable call like below. Thus increases the code coverage.
#Test(expected=DBException)
public void testMakeHelloWorldEntryInTable() {
//This can any third party library which cannot be configured for ut.
//One example is testing the AWS bucket exist or not.
instance.makeHelloWorldEntryInTable();
}
#Test
public void testPrintHelloWorld() {
Mockito.doNothing()
.when(localInstance)
.makeHelloWorldEntryInTable();
localInstance.printHelloWorld();
}
Case_3: if you have private method, then make it default package level and test it. Thus improves the code coverage.

Mocking file reading/writing via JUnit

How do you mock file reading/writing via JUnit?
Here is my scenario
MyHandler.java
public abstract class MyHandler {
private String path = //..path/to/file/here
public synchronized void writeToFile(String infoText) {
// Some processing
// Writing to File Here
File file = FileUtils.getFile(filepath);
file.createNewFile();
// file can't be written, throw FileWriteException
if (file.canWrite()) {
FileUtils.writeByteArrayToFile(file, infoText.getBytes(Charsets.UTF_8));
} else {
throw new FileWriteException();
}
}
public String readFromFile() {
// Reading from File here
String infoText = "";
File file = new File(path);
// file can't be read, throw FileReadException
if (file.canRead()) {
infoText = FileUtils.readFileToString(file, Charsets.UTF_8);
} else {
throw FileReadException();
}
return infoText
}
}
MyHandlerTest.java
#RunWith(PowerMockRunner.class)
#PrepareForTest({
MyHandler.class
})
public class MyHandlerTest {
private static MyHandler handler = null;
// Some Initialization for JUnit (i.e #Before, #BeforeClass, #After, etc)
#Test(expected = FileWriteException.class)
public void writeFileTest() throws Exception {
handler.writeToFile("Test Write!");
}
#Test(expected = FileReadException.class)
public void readFileTest() throws Exception {
handler.readFromFile();
}
}
Given above source, Scenario when file is not writable (write permission not allowed) is OK, However, when i try to do scenario wherein file is not readable (read permission not allowed). It always read the file, i have already tried to modify the file permission on the test code via below
File f = new File("..path/to/file/here");
f.setReadable(false);
However, I did some reading, setReadable() always returns false (failed) when run on Windows machine.
Is there a way to modify the file permission of the target file programmatically in relation to JUnit?
Note
Target source code to test cannot be modified, meaning
Myhandler.class is a legacy code which is not to be modified.
Instead of relying on the operating system file permissions, use PowerMock to mock FileUtils.getFile(...) and make it return an instance of File (e.g. anonymous sub class) that returns a specific value for canWrite()/canRead().
Mocking static methods with Mockito
Since Mockito cannot mock static methods, use a File factory instead (or refactor your FileUtils to be a factory), then you can mock it and return a mocked File instance as well, where you can also mock any File methods you want.
So instead of FileUtils.getFile(filepath) you will now have something like FileFactory.getInstance().getFile(filepath) for example, where you can mock getFile(String) method easily.
In jUnit there's a handy rule for scenarios like yours.
public class MyHandlerTest {
#Rule
// creates a temp folder that will be removed after each test
public org.junit.rules.TemporaryFolder folder = new org.junit.rules.TemporaryFolder();
private MyHandler handler;
#Before
public void setUp() throws Exception {
File file = folder.newFile("myFile.txt");
// do whatever you need with it - fill with test content and so on.
handler = new MyHandler(file.getAbsolutePath()); // use the real thing
}
// Test whatever behaviour you need with a real file and predefined dataset.
}

Camel file unit test

I am new to Apache Camel, I have written a simple route to scan a directory (/test), file will be processed when it was copied into the directory. Anyone has an idea on how to write a camel unit test to test the following route? Is there a way to mock the process of copying the file into the /test directory so that the route will be triggered.
public void configure() {
from( "file:/test?preMove=IN_PROGRESS" +
"&move=completed/${date:now:yyyyMMdd}/${file:name}" +
"&moveFailed=FAILED/${file:name.noext}-${date:now:yyyyMMddHHmmssSSS}.${file:ext}" )
.process(new Processor() {
public void process(Exchange exchange) throws IOException {
File file = (File) exchange.getIn().getBody();
// read file content ......
}
});
}
You have done the routing by one of many correct ways. But there exist some more important pieces to make your code run - you should create a context, create a router with this your configure(), add it to a context, and run this context.
Sorry, I prefer beans to processors, so you have also to register a bean. And make you processing a normal named method in a named class.
I think, the most compact info is here. JUnit test is a standalone app and you need to run Camel as a standalone app for JUnit testing.
I think the basic idea is that you mock the end endpoint so you can check what is coming out your route. There are a few different ways, but you could test your route as follows:
public class MyRouteTest extends CamelSpringTestSupport {
private static final String INPUT_FILE = "myInputFile.xml";
private static final String URI_START = "direct:start";
private static final String URI_END = "mock:end";
#Override
public boolean isUseAdviceWith() {
return true;
}
#Override
protected AbstractApplicationContext createApplicationContext() {
return new AnnotationConfigApplicationContext(CamelTestConfig.class); // this is my Spring test config, where you wire beans
}
#Override
protected RouteBuilder createRouteBuilder() {
MyRoute route = new MyRoute();
route.setFrom(URI_START); // I have added getter and setters to MyRoute so I can mock 'start' and 'end'
route.setTo(URI_END);
return route;
}
#Test
public void testMyRoute() throws Exception {
MockEndpoint result = getMockEndpoint(URI_END);
context.start();
// I am just checking I receive 5 messages, but you should actually check the content with expectedBodiesReceived() depending on what your processor does to the those files.
result.expectedMessageCount(5);
// I am just sending the same file 5 times
for (int i = 0; i < 5; i++) {
template.sendBody(URI_START, getInputFile(INPUT_FILE));
}
result.assertIsSatisfied();
context.stop();
}
private File getInputFile(String name) throws URISyntaxException, IOException {
return FileUtils.getFile("src", "test", "resources", name);
}
I am sure you already solved your issue is 2013, but this is how I would solve it in 2017. Regards

PowerMock's expectNew() isn't mocking a constructor as expected

I'm trying to learn the ins and outs of various mocking libraries and PowerMock(specifically the EasyMock extension) is next on the list. I'm attempting to mock a constructor and the examples provided don't have the same response when I try to replicate them. As far as I can tell, it never mocks the constructor and just proceeds as if it were normal.
This is the test class:
#RunWith(PowerMockRunner.class)
#PrepareForTest({Writer.class})
public class FaultInjectionSituationTest {
#Test
public void testActionFail() throws Exception {
FaultInjectionSituation fis = new FaultInjectionSituation();
PowerMock.expectNew(Writer.class, "test")
.andThrow(new IOException("thrown from mock"));
PowerMock.replay(Writer.class);
System.out.println(fis.action());
PowerMock.verify(Writer.class);
}
}
I've tried replacing the "test" with an EasyMock.isA(String.class), but it yielded the same results.
This is the FaultInjectionSituation:
public class FaultInjectionSituation {
public String action(){
Writer w;
try {
w = new Writer("test");
} catch (IOException e) {
System.out.println("thrown: " + e.getMessage());
return e.getLocalizedMessage();
}
return "returned without throw";
}
}
The "Writer" is nothing more than a shell of a class:
public class Writer {
public Writer(String s) throws IOException {
}
public Writer() throws IOException{
}
}
When the test is run, it prints out "returned without throw", indicating the exception was never thrown.
You need to prepare the class that is calling the constructor as well, so PowerMock knows to expect a mocked constructor call. Try updating your code with the following:
#RunWith(PowerMockRunner.class)
#PrepareForTest({Writer.class, FaultInjectionSituation.class})
public class FaultInjectionSituationTest {
// as before
}
You need to first create a mock object:
Writer mockWriter = PowerMock.createMock(Writer.class)
PowerMock.expectNew(Writer.class, "test").andReturn(mockWriter)

Why this file can't be deleted?

I wrote unit test (JUnit 4) that performs some logic and writes result to file. In #Before annotated method it creates file and in #After the file should be deleted. It isn't though, and I can't figure it out, why.
I am using Google Guava 10.01 Files API. Here's my unit test code:
public class CashierTest extends ContextedTest {
private File cashierFile;
#Before
public void createFile() throws Exception {
cashierFile = new File("D://workspace-sts/spring-miso/cashier.txt");
cashierFile.createNewFile();
}
#After
public void release() {
if (cashierFile.exists()) {
if (!cashierFile.delete()) {
System.out.println("Couldn't delete cashier file");
}
}
cashierFile = null;
}
#Test
public void testCashier() throws Exception {
// file shouldn't contain any text
assertFalse(Files.toString(cashierFile, Charset.defaultCharset()).length() > 0);
Cashier cashier = (Cashier) context.getBean("cashier");
ShoppingCart cart = (ShoppingCart) context.getBean("shoppingCartPrototype");
cashier.checkout(cart);
assertTrue(cashierFile.exists());
// file should contain text now
assertTrue(Files.toString(cashierFile, Charset.defaultCharset()).length() > 0);
}
#Override
protected void setPath() {
path = "sk/xorty/advancedioc/beans.xml";
}
}
Note: ContextedTest superclass is my test which holds Spring container it isn't relevant atm.
Simply instanting a File does not mean that an actual file will be created. Call createNewFile() or createTempFile() on that instance for this.
Within your test method you don't seem to pass that file reference to anyone that could possibly create the file or write anything in it... Am I missing something or is the code you posted missing some key lines ?
You should use the TemporaryFolder Rule with JUnit 4. This will handle the setup and teardown of temporary test directories and files.
public static class HasTempFolder {
#Rule public TemporaryFolder folder= new TemporaryFolder();
#Test public void testUsingTempFolder() throws IOException {
File createdFile= folder.newFile("myfile.txt");
...
}
}
Other Rules are part also part of Junit 4.

Categories

Resources