Easymock and Shiro - easymock

I am trying to use AbstractShiroTest abstract class for my unit tests as explained in http://shiro.apache.org/testing.html
I have my test class:
public class BeanTest extends AbstractShiroTest {
...
#Test
public void testShiro() {
Subject subjectUnderTest = createNiceMock(Subject.class);
expect(subjectUnderTest.isAuthenticated()).andReturn(true);
expect(subjectUnderTest.getPrincipal()).andReturn("cenap");
setSubject(subjectUnderTest);
assertTrue("Subject is not authenticated", SecurityUtils.getSubject().isAuthenticated());
assertNotNull("Subject principle null", SecurityUtils.getSubject().getPrincipal());
}
#AfterClass
public static void tearDownClass() {
tearDownShiro();
}
Both assertions fail... SecurityUtils.getSubject() returns some object but isAuthenticated() method of that object returns false and getPrincipal() method returns null. "expect" clauses do not seem to work. What am I missing?

Answering my own question as it might help someone. It was a stupid mistake...
The trick is: Do not forget adding
anyTimes();
after your expect statement and so not forget calling
replay (subjectUnderTest);
after that!
So your BeforeClass method should be like:
#BeforeClass
public static void setUpShiro() {
Subject subjectUnderTest = createNiceMock(Subject.class);
expect(subjectUnderTest.isAuthenticated()).andReturn(true).anyTimes();
expect(subjectUnderTest.getPrincipal()).andReturn(Admin).anyTimes();
setSubject(subjectUnderTest);
replay (subjectUnderTest);
}

Related

How to skip invoking another method to create a mock object?

I have a class of methods, if I want to test one method, but the objects created in this method depend on other methods in the class. I don't want to actually call the other methods, I just want to create mock objects to test. How should I do it?
#Test
public void testLoadException(String id) {
WorkflowExecution mockWorkflowExection = getWorkflowExecution(id);
}
I tried to just do so
WorkflowExecution mockExecution = EasyMock.create(WorkflowExecution.class);
Easy.expect(this.test.getWorkflowExecution(EasyMock.anyString())).andReturn(mockExecution);
But this does not work, the test gives me Nullpointer exception.
So can I skip the calling of getWorkflowExecution(id), Thanks!
You need a partial mock. Your example doesn't make much sense. So here is what you seem to want.
public class WorkflowExecution {
public void theRealMethodIWantToCall() {
theMethodIWantToMock();
}
void theMethodIWantToMock() {
}
}
#Test
public void testLoadException() {
WorkflowExecution execution = partialMockBuilder(WorkflowExecution.class)
.addMockedMethod("theMethodIWantToMock")
.mock();
execution.theMethodIWantToMock();
replay(execution);
execution.theRealMethodIWantToCall();
verify(execution);
}

Calling #Test method in a Test method or Calling a class in a Test method?

As i posted earlier, we can't put a test code in After method or Before method.
My scenario is like i have 10 test methods after each method i have to run another test method.
Please let me know the answer for this if one knows...
Thanks in Advance.
You can use #Test methods but specify the priority to execute.
#Test( priority = 1 )
public void test1() {
System.out.println("test1");
}
#Test( priority = 2 )
public void test2() {
System.out.println("test2");
}
#Test( priority = 3 )
public void test3() {
System.out.println("test3");
}
in this way you can executed test methods on specified order. I hope it will helps you.
Thank You,
Murali
You need to follow a basic java code, call methods from One class in to Another
Class A
public class A {
static void method1()
{
System.out.println("Selenium_1");
}
static void method2()
{
System.out.println("Selenium_1");
}
}
Class B
public class B extends A {
public static void main(String ar[])
{
method1();
method2();
}
}
Hope this solution may help your problem.

what's the difference between object and primitive type when using matchers in EasyMock

//service to mock
public interface ServiceToMock {
public void operateDouble(Double dbValue);
public void operateCar(Car car);
}
//class under test
public class ClassUnderTest {
ServiceToMock service;
public void operateDouble(Double dbValue){
service.operateDouble(dbValue);
}
public void operateObject(Car car){
service.operateCar(car);
}
}
//unit test class
#RunWith(EasyMockRunner.class)
public class TestEasyMockMatcherUnderTest {
#TestSubject
private final ClassUnderTest easyMockMatcherUnderTest = new ClassUnderTest();
#Mock
private ServiceToMock mock;
#Test
public void testOperateCar() {
//record
mock.operateCar(EasyMock.anyObject(Car.class));
EasyMock.expectLastCall();
// replay
EasyMock.replay(mock);
//matcher here...
easyMockMatcherUnderTest.operateObject(EasyMock.anyObject(Car.class));
//easyMockMatcherUnderTest.operateObject(new Car());
// verify
EasyMock.verify(mock);
}
#Test
public void testOperateDouble() {
// record
mock.operateDouble(EasyMock.anyDouble());
EasyMock.expectLastCall();
// replay
EasyMock.replay(mock);
easyMockMatcherUnderTest.operateDouble(EasyMock.anyDouble());
// verify
EasyMock.verify(mock);
}
}
As the above code has shown, I intent to test two methods(operateDouble, operateObject). But things are kinda weird since everything runs fine in the operateDouble block while the compiler complaints an "Illegal state exception: 1 matchers expected, 2 recored." when runnig operateObject. And if commentting the method operateDouble out, the compaint goes away..So what is the difference between Double and my custom object Car, as the Double can also be considered as an object. And why does codes in operateObject runs well when commenting operateDouble method out?
EasyMock.anyDouble and EasyMock.anyObject are not meant to be used in replay mode. They are used to setup your expectations in record mode.
Use this in your first test (testOperateCar):
easyMockMatcherUnderTest.operateObject(new Car());
and something like this in your second (testOperateDouble):
easyMockMatcherUnderTest.operateDouble(1.0);
By the way, you don't need to call EasyMock.expectLastCall. It is only useful if you expect a void method to be called multiple times, for example:
mock.operateCar(EasyMock.anyObject(Car.class));
EasyMock.expectLastCall().times(3);

TestNg Assert.AssertTrue Always returns False - Selenium Webdriver

I have a util function as below:
public static boolean isWebElementEnabled(WebElement element) {
try {
return element.isEnabled();
} catch (Exception exx) {
return false;
}
}
public static boolean chkForThisElement(WebElement myElement) {
try {
return myElement.isDisplayed();
} catch (Exception e) {
// TODO Auto-generated catch block
return false;
}
}
I call it like this in the base class:
public static boolean isusernameBoxEnabled = isWebElementEnabled(unameBox);
public static boolean ispWordBoxEnabled = isWebElementEnabled(pwordBox);
public static boolean issubmitBtnEnabled = isWebElementEnabled(submitBtn);
public static boolean isctrsDrpdwnEnabled = isWebElementEnabled(multyCts);
When I test it in the Test class, it always returns false. I tried diff ways of testing for existence, but it only returns false.
#Test(priority=1)
public void verifyLoginpagecontrols() {
Assert.assertTrue(isusernameBoxEnabled);
Assert.assertTrue(ispWordBoxEnabled);
Assert.assertTrue(issubmitBtnEnabled);
Assert.assertTrue(isctrsDrpdwnEnabled);
}
i found a solution that works cool with Ff and Chromre driver nevertheless fails in Htmlunit driver.
Solution for the above problem -
// Initialize the home page elements and then check for assertions;
homePagePO searchPage = PageFactory.initElements(driver,
homePagePO.class);
Assert.assertTrue(chkForThisElement(searchPage.AccManagerHref));
Assert.assertTrue(chkForThisElement(searchPage.disHref));
Sorry to say but I find several things wrong with your code :-
You have not initialized the page factory. That is the reason why you are getting the null error.
In your comment, you have said that you are finding elements by using #findBy. But why have you decalared the WebElement as static?.
Why have you declared isusernameBoxEnabled and related boolean variables as global variables. You could use the isWebElementEnabled() function in your assert directly.
Basically your isWebElementEnabled() is not useful at all if you are using page factory.
Because the moment you use unameBox, selenium looks for the element in the webpage and if not found returns a noSuchElement Exception. So unameBox wont reach isWebElementEnabled() if it is not found in the webpage.
You said there is a base class and Test class. But I don't understand how your code works if there are different classes because you have not made a reference to static variable as Assert.assertTrue(baseClass.isusernameBoxEnabled). So I am assuming that you have only one class and different methods.
Try the following code :-
public class Base {
#FindBy()
WebElement unameBox;
#FindBy()
WebElement pwordBox;
#FindBy()
WebElement submitBtn;
#FindBy()
WebElement multyCts;
}
public class Test {
#Test(priority=1)
public void verifyLoginpagecontrols() {
//initialize page factory
Base base = PageFactory.initElements(driver, Base.class);
Assert.assertTrue(base.unameBox.isEnabled());
Assert.assertTrue(base.pwordBox.isEnabled());
Assert.assertTrue(base.submitBtn.isEnabled());
Assert.assertTrue(base.multyCts.isEnabled());
}
}
Hope this helps you.

Is it bad to set up singletons such that all methods are static methods?

I frequently setup singleton classes that are intended to be used by other programmers and I find that I'm not sure if there is a preferred way to setup access to methods in those classes. The two ways I've thought to do it are:
public class MyClass {
private static MyClass instance;
public static void DoStuff( ) {
instance.DoStuffInstance( );
}
private void DoStuffInstance( ) {
// Stuff happens here...
}
}
where the usage is: MyClass.DoStuff( );
or something more like this:
public class MyClass {
public static MyClass instance;
public void DoStuff( ) {
// Stuff happens here...
}
}
where the usage is: MyClass.instance.DoStuff( );
Personally, I tend to prefer the first option. I find that having MyClass.instance all over the place is both ugly and unintuitive to remember for less experienced programmers.
Is there any good reason to prefer one of these over the other? Opinions are fine. Just curious what others think.
I've never seen a Singleton implemented this way. A typical setup might be something like this:
public class MyClass {
private static final MyClass instance = null;
// Private to ensure that no other instances can be allocated.
private MyClass() {}
// Not thread safe!
public static MyClass getInstance() {
if( instance == null ) {
instance = new MyClass();
}
return instance;
}
public void DoStuff( ) {
// Stuff happens here...
}
}
This way all of the calls will be similar to instance.DoStuff(); and you will only need to define one method per "operation", rather than needing a static method and then the actual "instance" method that your first approach uses.
Also, the way you have it set up, it looks like you can call those static methods before the instance is actually initialized, which is a problem.

Resources