Skip to content

Instantly share code, notes, and snippets.

@philosodad
Last active May 18, 2017 06:24
Show Gist options
  • Save philosodad/5212826 to your computer and use it in GitHub Desktop.
Save philosodad/5212826 to your computer and use it in GitHub Desktop.
This gist is a solution to a code Kata I've used to teach a little bit about Mocking and Stubbing. The idea of the Kata is to use TDD to develop a method that will fetch a SVN repository and get the names of each directory entry in the repository. SvnHelperTest uses mocks, stubs, and partial mocks (spys) to test the method. The last time I taugh…
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jpauldaigle.svnkit1</groupId>
<artifactId>svnkit1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>svnkit1</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>org.tmatesoft.svnkit</id>
<url>http://maven.tmatesoft.com/content/repositories/releases/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
<version>1.7.8</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.4</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
</project>
package svnhelpers;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.TransformerUtils;
import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
public class SvnHelper {
private SVNRepository internalRepo;
public SvnHelper(String repoLocation) throws SVNException {
internalRepo = SVNRepositoryFactory.create(SVNURL
.parseURIDecoded(repoLocation));
}
public SVNRepository getInternalRepo() {
return internalRepo;
}
public ArrayList<String> getNamesFromRepo() throws SVNException {
SVNRepository inRepo = this.getInternalRepo();
Collection directory = this.getLatestDir(inRepo);
return (ArrayList<String>) CollectionUtils.collect(
directory,
TransformerUtils.invokerTransformer("getName"));
}
private Collection getLatestDir(SVNRepository repo) throws SVNException{
return repo.getDir("", repo.getLatestRevision(), null, new ArrayList<String>());
}
}
package svnhelpers;
import java.util.ArrayList;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNProperties;
import org.tmatesoft.svn.core.io.SVNRepository;
public class SvnHelperTest{
@Test
public void shouldCallGetDir() throws SVNException {
SvnHelper myHelper = new SvnHelper("http://any.old.string");
SvnHelper spyHelper = Mockito.spy(myHelper);
SVNRepository myRepo = Mockito.mock(SVNRepository.class);
Mockito.when(spyHelper.getInternalRepo()).thenReturn(myRepo);
Mockito.when(myRepo.getLatestRevision()).thenReturn((long) 0);
spyHelper.getNamesFromHeadOf();
Mockito.verify(myRepo).getDir(Mockito.anyString(), Mockito.anyLong(), Mockito.any(SVNProperties.class), Mockito.anyCollection());
}
@Test
public void shouldReturnTheListOfNames() throws SVNException{
SvnHelper myHelper = new SvnHelper("http://any.old.string");
SvnHelper spyHelper = Mockito.spy(myHelper);
SVNRepository myRepo = Mockito.mock(SVNRepository.class);
Mockito.when(spyHelper.getInternalRepo()).thenReturn(myRepo);
String name1 = "Bob";
String name2 = "Albert";
SVNDirEntry dirEntry1 = Mockito.mock(SVNDirEntry.class);
SVNDirEntry dirEntry2 = Mockito.mock(SVNDirEntry.class);
Mockito.when(dirEntry1.getName()).thenReturn(name1);
Mockito.when(dirEntry2.getName()).thenReturn(name2);
ArrayList testArray = new ArrayList();
testArray.add(dirEntry1);
testArray.add(dirEntry2);
ArrayList expectedArray = new ArrayList();
expectedArray.add(name1);
expectedArray.add(name2);
Mockito.when(myRepo.getDir(Mockito.anyString(),Mockito.anyLong(), Mockito.any(SVNProperties.class), Mockito.anyCollection())).thenReturn(testArray);
Assert.assertEquals(expectedArray, spyHelper.getNamesFromHeadOf());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment