I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Commit 4776080c authored by zzz's avatar zzz
Browse files

Merge branch 'test-context' into 'master'

ConvertToHash test I2PAppContext part

See merge request !15
parents c79e6455 79a868b8
No related branches found
No related tags found
1 merge request!15ConvertToHash test I2PAppContext part
package net.i2p;
public class TestContext extends I2PAppContext {
public TestContext() {
TestContext.setGlobalContext(this);
}
/**
* Allows overriding the existing I2PAppContext with a test context who's fields we may mock as we like
*
* @param ctx Our test context to replace the global context with
*/
public static void setGlobalContext(TestContext ctx){
_globalAppContext = ctx;
}
}
package net.i2p.util;
import net.i2p.TestContext;
import net.i2p.client.naming.NamingService;
import net.i2p.data.Destination;
import net.i2p.data.Hash;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
public class ConvertToHashMockTest{
@Mock private NamingService namingService;
@Mock private Destination destination;
@Mock private Hash hash;
@InjectMocks TestContext testContext;
@Before
public void before() {
MockitoAnnotations.initMocks(this);
}
/**
* Reset the global context after all tests in the class are done.
*
* We would otherwise pollute the other tests that depend on I2PAppContext
*/
@AfterClass
public static void afterClass(){
TestContext.setGlobalContext(null);
}
@Test
public void testMockedDestination() {
when(namingService.lookup("zzz.i2p")).thenReturn(destination);
when(destination.calculateHash()).thenReturn(hash);
assertSame(hash, ConvertToHash.getHash("zzz.i2p"));
verify(namingService).lookup("zzz.i2p");
verify(destination).calculateHash();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment