The abstract class has a child class, and I am mocking that. That is
just an empty class. So I am pasting the abstract class. I have also
added the test afterwards -
public abstract class AbstractZipFileScannable implements Scannable {
private static final Log log = LogFactory.getLog
(AbstractZipFileScannable.class);
private File sourceFile;
private BundleManager bundleManager;
private ScanRecord lastBuild;
private ScanRecord lastPreBuild;
@Override
public void scan() {
if (!sourceFile.exists()) {
return;
}
ScanRecord tempBuild = new ScanRecord();
tempBuild.setTarget(sourceFile);
if (lastPreBuild == null) {
lastPreBuild = tempBuild;
return;
}
if (lastPreBuild.equals(tempBuild)) {
if (lastBuild.getLastModified() < lastPreBuild.getLastModified()) {
File tempExtractedDirectory = new File(System.getProperty
("java.io.tmpdir") + File.separator + System.currentTimeMillis() + "_"
+ sourceFile.getName());
File extractedLocation = extract(sourceFile,
tempExtractedDirectory);
lastPreBuild = null;
lastBuild = tempBuild;
sourceFile.delete();
bundleManager.distributeEvent(extractedLocation);
}
} else {
lastPreBuild = tempBuild;
}
}
public void preScanStarts() {
if (sourceFile != null && sourceFile.exists()) {
sourceFile.delete();
}
}
File extract(File sourceZipFile, File extractedDirectory) {
FileUtil.deleteDirectory(extractedDirectory);
try {
ZipManager zipManager = new ZipManager();
zipManager.explodeArchive(sourceZipFile, extractedDirectory.getPath
());
} catch (IOException e) {
log.error(e);
e.printStackTrace();
// throw new RuntimeException(e);
}
return extractedDirectory;
}
public void setSourceFile(File sourceFile) {
Validate.notNull(sourceFile, "Source zip file can not be null");
this.sourceFile = sourceFile;
ScanRecord scanTarget=new ScanRecord();
scanTarget.setTarget(sourceFile);
this.lastBuild=scanTarget;
}
public void setBundleManager(BundleManager bundleManager) {
this.bundleManager = bundleManager;
}
}
ublic class TestZipBundleScannable {
@Mock
private BundleManager bundleManager;
private PDIBundleScannable pdiBundleScannable;
@Before
public void init() {
pdiBundleScannable = new PDIBundleScannable();
pdiBundleScannable.setBundleManager(bundleManager);
}
@Test
public void testScan() {
File sourceFile = mock(File.class);
when(sourceFile.lastModified()).thenReturn(10l);
when(sourceFile.length()).thenReturn(100l);
when(sourceFile.exists()).thenReturn(true);
when(sourceFile.delete()).thenReturn(true);
// make initial call
pdiBundleScannable.setSourceFile(sourceFile);
PDIBundleScannable spy=spy(pdiBundleScannable);
doReturn(sourceFile).when(spy).extract(sourceFile, sourceFile);
//when(spy.extract(sourceFile, sourceFile)).thenReturn(sourceFile);
//deleting the file, so returning false
when(sourceFile.exists()).thenReturn(false);
spy.scan();
//considering new file is being copied (but not totally copied)
when(sourceFile.exists()).thenReturn(true);
when(sourceFile.lastModified()).thenReturn(12l);
when(sourceFile.length()).thenReturn(10l);
spy.scan();
//considering file copy is done
when(sourceFile.lastModified()).thenReturn(15l);
when(sourceFile.length()).thenReturn(101l);
spy.scan();
//two consecutive invocation to indicate its done
spy.scan();
}
}
In the scan method, there is "extract" method, which is invoked, and
that's the one i mocked. so that i can only check the scan method.
Sorry if I was doing something wrong. I tried to go by the examples
given on mockito site.
Thanks again.
On Jul 1, 12:47 pm, Bartosz Bańkowski <
bbankow...@gmail.com> wrote:
> Can you copy&paste full source code? The problem here is also that in
> when() you are not using your spy, but I'm not sure if it is a typo or
> is it a real cause.
>
> Bartosz
>
> 2009/7/1 jahid <
jsho...@gmail.com>: