mercredi 4 mars 2015

Mock Service object autowired in bean which uses apache shiro


I am trying to create a mock for Service layer which is autowired in my managed beans.


I am using apache shiro, jsf 2.1 and primefaces 4.0. I have referred


http://ift.tt/1DSSnP5


http://ift.tt/1Nf0ISt


but when i create mocks like



@InjectMocks
ServiceCalendarViewBean serviceCalendarViewBean;// = new ServiceCalendarViewBean();


,I get a exception



org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.ThreadContext or as a vm static singleton. This is an invalid application configuration.
at org.apache.shiro.SecurityUtils.getSecurityManager(SecurityUtils.java:123)
at org.apache.shiro.subject.Subject$Builder.<init>(Subject.java:627)
at org.apache.shiro.SecurityUtils.getSubject(SecurityUtils.java:56)
at com.fetchinglife.modules.service.views.ServiceCalendarViewBean.<init>(ServiceCalendarViewBean.java:151)
at com.fetchinglife.application.modules.ServiceCalendarViewTestBean.<init>(ServiceCalendarViewTestBean.java:53)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:195)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:244)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:241)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
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:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)


What i did till now..



@RunWith(MockitoJUnitRunner.class) //to write a unit test
//@RunWith(SpringJUnit4ClassRunner.class) //is meant for integration test
@ContextConfiguration(locations = { "file:test/resources/application-test-config.xml" })
public class ServiceCalendarViewTestBean extends AbstractShiroTest {

@Autowired
private SessionFactory sessionFactory;

@Mock
ServiceCalendarViewService iServiceCalendarViewService;

@InjectMocks
ServiceCalendarViewBean serviceCalendarViewBean;// = new ServiceCalendarViewBean();

@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);

//1. Create a mock authenticated Subject instance for the test to run:
Subject subjectUnderTest = Mockito.mock(Subject.class);
Mockito.when(subjectUnderTest.isAuthenticated()).thenReturn(true);

//2. Bind the subject to the current thread:
setSubject(subjectUnderTest);
}

@Test
public void createServiceMiniPopUpUsingDefaultServiceType() {
serviceCalendarViewBean = new ServiceCalendarViewBean();
serviceCalendarViewBean.getSelectedPetsAutoComp().addAll(serviceCalendarViewBean.completedName("Emma") );

}


}


Bean



@Component
@Scope("session")
public class ServiceCalendarViewBean {

@Autowired
IServiceCalendarViewService iServiceCalendarViewService;

Session session = SecurityUtils.getSubject().getSession();

public void completePet(String name) {
String id = iServiceCalendarViewService.getSQLStyleList(name);
}


Also the service class has many autowired repositories which also have to be mocked. I need help for that also. In short, the thing is i want to create a mock for my autowired service class and inject the same to the component. Any help will be appreciated.





Aucun commentaire:

Enregistrer un commentaire