See code below...
package com.abc.web.user.manager;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.bson.types.ObjectId;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import com.abc.web.user.dao.AccountDao;
import com.abc.web.user.dao.AddressDao;
import com.abc.web.user.dao.CallerDao;
import com.abc.web.user.dao.PaymentDao;
import com.abc.web.user.entity.Account;
import com.abc.web.user.entity.EntityTestSupport;
import com.abc.web.user.entity.Caller;
import com.abc.web.user.exception.AppException;
import com.abc.web.user.util.ApplicationProperties;
public class WalkFederatedAccountsTest
{
private static final String SCHMUCK_EMAIL =
"sch...@abccorp.com";
private static final String PASSWORD = "test123";
private static final String ABC_CORP_NAME = "ABC Corp.";
private static final String ACMECORP_NAME = "Acme Corp.";
private static final String ACMELTD_NAME = "Acme, Ltd.";
private static final String ACMEASS_NAME = "Acme Associates,
Inc.";
private static final String WALMART_NAME = "Walmart";
private static final String ABC_CORP_ENTITY =
"AbcCorp-entitykey";
private static final String ACMECORP_ENTITY =
"Caller1-entitykey";
private static final String ACMELTD_ENTITY =
"Caller2-entitykey";
private static final String ACMEASS_ENTITY =
"Caller3-entitykey";
private static final String WALMART_ENTITY =
"Caller4-entitykey";
private static final ObjectId ABC_CORPOID = new ObjectId(
"000000000000000000000099" );
private static final ObjectId ACMECORPOID = new ObjectId(
"000000000000000000000011" );
private static final ObjectId ACMELTDOID = new ObjectId(
"000000000000000000000022" );
private static final ObjectId ACMEASSOID = new ObjectId(
"000000000000000000000033" );
private static final ObjectId WALMARTOID = new ObjectId(
"000000000000000000000044" );
private static Caller ABC_CORP;
private static Caller ACMECORP;
private static Caller ACMELTD;
private static Caller ACMEASS;
private static Caller WALMART;
private static Account SCHMUCK;
private static Account ACCOUNT1;
private static Account ACCOUNT2;
private static Account ACCOUNT3;
private static Account ACCOUNT4;
private WalkFederatedAccounts walker;
@Mock private AccountDao accountDao;
@Mock private AddressDao addressDao;
@Mock private PaymentDao paymentDao;
@Mock private CallerDao callerDao;
@Before
public void setup()
{
ApplicationProperties.reloadProperties();
MockitoAnnotations.initMocks( this );
new StaticDataDaoMocks();
EntityTestSupport.setEntityDaoMocks( accountDao, addressDao,
paymentDao, callerDao );
walker = new WalkFederatedAccounts();
ABC_CORP = EntityTestSupport.constructCaller(
ABC_CORPOID, ABC_CORP_ENTITY, ABC_CORP_NAME );
ACMECORP = EntityTestSupport.constructCaller(
ACMECORPOID, ACMECORP_ENTITY, ACMECORP_NAME );
ACMELTD = EntityTestSupport.constructCaller(
ACMELTDOID, ACMELTD_ENTITY, ACMELTD_NAME );
ACMEASS = EntityTestSupport.constructCaller(
ACMEASSOID, ACMEASS_ENTITY, ACMEASS_NAME );
WALMART = EntityTestSupport.constructCaller(
WALMARTOID, WALMART_ENTITY, WALMART_NAME );
SCHMUCK = EntityTestSupport.constructAccount(
SCHMUCK_EMAIL, ABC_CORPOID, PASSWORD );
ACCOUNT1 = EntityTestSupport.constructAccount(
SCHMUCK_EMAIL, ACMECORPOID, PASSWORD );
ACCOUNT2 = EntityTestSupport.constructAccount(
SCHMUCK_EMAIL, ACMELTDOID, PASSWORD );
ACCOUNT3 = EntityTestSupport.constructAccount(
SCHMUCK_EMAIL, ACMEASSOID, PASSWORD );
ACCOUNT4 = EntityTestSupport.constructAccount(
SCHMUCK_EMAIL, WALMARTOID, PASSWORD );
EntityTestSupport.addToCallerdata( ABC_CORP,
ACMECORP_ENTITY, true, true, true, true, false );
EntityTestSupport.addToCallerdata( ABC_CORP,
ACMELTD_ENTITY, true, true, true, true, false );
EntityTestSupport.addToCallerdata( ABC_CORP,
ACMEASS_ENTITY, true, false, true, true, false );
EntityTestSupport.addToCallerdata( ABC_CORP,
WALMART_ENTITY, false, false, false, false, false );
EntityTestSupport.addToCallerdata( ACMECORP,
ABC_CORP_ENTITY, true, true, true, true, false );
EntityTestSupport.addToCallerdata( ACMELTD,
ABC_CORP_ENTITY, true, true, true, true, false );
EntityTestSupport.addToCallerdata( ACMEASS,
ABC_CORP_ENTITY, true, false, true, true, false );
EntityTestSupport.addToCallerdata( WALMART,
ABC_CORP_ENTITY, false, false, false, false, false );
SCHMUCK.setFullname( "Schmuck at ABC Corp." );
ACCOUNT1.setFullname( "Schmuck at Acme Corp." );
ACCOUNT2.setFullname( "Schmuck at Acme, Ltd.." );
ACCOUNT3.setFullname( "Schmuck at Acme Associates, Inc."
);
ACCOUNT4.setFullname( "Schmuck at Walmart" );
}
@Test
public void testWalkAccounts_bypassword() throws AppException
{
List< Account > accounts = new ArrayList< Account
>();
accounts.add( SCHMUCK );
accounts.add( ACCOUNT1 );
accounts.add( ACCOUNT2 );
accounts.add( ACCOUNT3 );
accounts.add( ACCOUNT4 );
// now mock the DAO methods called by CallerManager and
AccountManager...
when( accountDao.readAllByIdentity( SCHMUCK_EMAIL )
).thenReturn( accounts ); this is mocked...
when( callerDao.readByOid( ABC_CORPOID ) ).thenReturn(
ABC_CORP );
when( callerDao.readByOid( ACMECORPOID ) ).thenReturn(
ACMECORP );
when( callerDao.readByOid( ACMELTDOID ) ).thenReturn(
ACMELTD );
when( callerDao.readByOid( ACMEASSOID ) ).thenReturn(
ACMEASS );
when( callerDao.readByOid( WALMARTOID ) ).thenReturn(
WALMART );
walker.setCallingcalleroid( ABC_CORPOID );
walker.setCallingcaller( callerDao.readByOid( ABC_CORPOID )
);
walker.setIdentity( SCHMUCK_EMAIL );
walker.setObject(
"schmuck-...@schmucks-r-us.com" );
walker.byPassword( new Date() );
...and called inside
here.
// these calls into the DAO should have been made during
this test...
verify( callerDao ).readByOid( ABC_CORPOID );
verify( accountDao ).readAllByIdentity( SCHMUCK_EMAIL
); --no error about this (and there shouldn't be)
verify( callerDao ).readByOid( ABC_CORPOID );
verify( callerDao ).readByOid( ACMECORPOID );
verify( callerDao ).readByOid( ACMELTDOID );
verify( callerDao ).readByOid( ACMEASSOID );
verify( callerDao ).readByOid( WALMARTOID );
verify( accountDao ).update( ACCOUNT1 );
verify( accountDao ).update( ACCOUNT2 );
verify( accountDao ).update( ACCOUNT3 );
}
@Test
public void testWalkAccounts_byemail() throws AppException
{
List< Account > accounts = new ArrayList< Account
>();
accounts.add( SCHMUCK );
accounts.add( ACCOUNT1 );
accounts.add( ACCOUNT2 );
accounts.add( ACCOUNT3 );
accounts.add( ACCOUNT4 );
// now mock the DAO methods called by CallerManager and
AccountManager...
when( accountDao.readAllByIdentity( SCHMUCK_EMAIL )
).thenReturn( accounts );
when( callerDao.readByOid( ABC_CORPOID ) ).thenReturn(
ABC_CORP );
when( callerDao.readByOid( ACMECORPOID ) ).thenReturn(
ACMECORP );
when( callerDao.readByOid( ACMELTDOID ) ).thenReturn(
ACMELTD );
when( callerDao.readByOid( ACMEASSOID ) ).thenReturn(
ACMEASS );
when( callerDao.readByOid( WALMARTOID ) ).thenReturn(
WALMART );
walker.setCallingcalleroid( ABC_CORPOID );
walker.setCallingcaller( callerDao.readByOid( ABC_CORPOID )
);
walker.setIdentity( SCHMUCK_EMAIL );
walker.setObject( "Test123" );
walker.byEmail( null );
<------
accountDao.readAllByIdentity() called inside here
// these calls into the DAO should have been made during
this test...
verify( callerDao ).readByOid( ABC_CORPOID );
verify( accountDao ).readAllByIdentity(
SCHMUCK_EMAIL ); --singled out as uncalled
verify( callerDao ).readByOid( ABC_CORPOID );
verify( callerDao ).readByOid( ACMECORPOID );
verify( callerDao ).readByOid( ACMELTDOID );
verify( callerDao ).readByOid( ACMEASSOID );
verify( callerDao ).readByOid( WALMARTOID );
verify( accountDao ).update( ACCOUNT1 );
verify( accountDao ).update( ACCOUNT2 );
verify( accountDao ).update( ACCOUNT3 );
}
}
Failure Trace
Wanted but not invoked:
accountDao.readAllByIdentity(
"sch...@abccorp.com"
);
-> at
com.abc.web.user.manager.WalkFederatedAccountsTest.testWalkAccounts_byemail(WalkFederatedAccountsTest.java:183)
Actually, there were zero
interactions with this mock.
at
com.abc.web.user.manager.WalkFederatedAccountsTest.testWalkAccounts_byemail(WalkFederatedAccountsTest.java:183)
at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at
java.lang.reflect.Method.invoke(Method.java:597)
at
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at
org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at
org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at
org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at
org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at
org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at
org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)