Pages

0 comments

Testing With EasyMock

This is more of a reference for me than anything else. But if you find it useful or want to add something, let me know.

There are 6 steps to testing a class with EasyMock.
1. Mock the class
2. Set expectation
3. Replay result
4. Execute testing method/function
5. Verify mock objects
6. Verify the result

So suppose I have the following class

public class Bakery{

protected MyOven oven;

public Bakery(){
oven = new MyOven();
}

public CookedFood cook(Ingredient ingredient) throws OvenException{
CookedFood cookedFood = oven.bake(ingredient);
}
}

(Yes, I'm obviously very creative with my naming scheme.)

Looking at this class, it's obvious that you want to mock out MyOven. But you also want to control what the the oven is returning, so you would also want to mock CookedFood.

Here's the sample code of how I would test the class.

public class BakeryTest{

private MyOven mockOven;
private Bakery bakery;

@Before
public void setUp(){
bakery = new Bakery();
mockOven = EasyMock.createMock(MyOven.class); //1. Create a mock object of the class you want to control
bakery.oven = mockOven; //Tell bakery to use your mockOven.
}

@After
public void tearDown(){
bakery = null;
mockOven = null;
}

@Test
public void testCook(){
CookedFood cookedFood = getCookedFood("cookie");

EasyMock.expect(mockOven.cook(isA(Ingredient.class)).andReturn(cookedFood).anyTime(); //2. Set expectation
EasyMock.replay(mockOven); //3 replay object

CookedFood actual = Bakery.cook(ingredient); //4 Execute testing method

verify(mockOven, cookedFood); //5. Make sure it was actually using your object

Assert.equals(actual.getType


}

@Test(expected=MyOvenException.class)
public void testCookWithException{

EasyMock.expect(mockOven.cook(isA(Ingredient.class)).andReturn
}

//I'm creating a CookedFood object here. You can mock the class or just create a new instance of it, it doesn't matter.
private CookedFood getCookedFood(String type){
CookedFood cookedFood = new CookedFood();
cookedFood.setType(type);
return cookedFood;
}

//Or you can mock the object. It really depends on how much you trust the object. I generally don't, so I use this.
private CookedFood getCookedFood(String type){
CookedFood cookedFood = EasyMock.createMock(CookedFood.class); //1. Mock the class
EasyMock.expect(cookedFood.getType()).andReturn(type).anyTime(); //2. Set expectation
EasyMock.replay(cookedFood); //Replay the mock class
}

}

Note1: The regular EasyMock can only mock interfaces. But if you want to mock an impl, use easymockextension. I generally use easymockextension for everything. When the easymockextension doesn't have a method that I want (e.g. verify), then I use the regular EasyMock. (Look at my import to see.)

Note2: I'm using JUnit 4. That's why I'm using annotation (@Test) instead of extending TestCase.

Note3: The way I do things is not the standard. I don't know what the standard is. If you do know, please point me to that direction.

1 comments

EasyMock

EasyMock.createMockBuilder(Cookie.class).addMockedMethod("getIngredients").createMock();
EasyMock.expect(cookie.getIngredients()).andReturn(getMockIngredients()).anyTimes();
EasyMock.replay(cookie);

0 comments

Autowiring Spring Method in XML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
    "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
    <!-- Beans to retrieve Persistence Manager -->
<!-- Static method -->
    <bean id="persistenceManagerFactory" class="javax.jdo.JDOHelper"
        factory-method="getPersistenceManagerFactory">
        <constructor-arg value="transactions-optional" />
    </bean>


<!-- Non static method -->
    <bean id="persistenceManager" factory-bean="persistenceManagerFactory"
        factory-method="getPersistenceManager" destroy-method="close">
    </bean>
</beans>

0 comments

Spring Notes

Autowiring
1. Does setter injection (if an empty constructor was available). Constructor second.
2. By setting a class as @Component, the id is automatically the class name with the first letter in lower case. (e.g. Class Person Spring id is "person").
3. You can overwrite autowiring with xml files
4. Must have "context:component-scan base-package="package name" />" in the xml to tell Spring there are beans to scan
5. @Autowire wires by type, not by name

0 comments

Custom Spring Property Editor

Note: There's only one instance of property editor for each bean. So if you use the same bean (even if it's a prototype), it uses the same instance of that object. Solution: create two beans or don't use PropertyEditor.

Code

package com.blogspot.sourie;

import java.beans.PropertyEditorSupport;

public class SocialSecurityPropertyEditor extends PropertyEditorSupport {

@Override
public void setAsText(String value){
if (value != null){
value = value.replace(" ", "");
if (value.length() == 9){
Integer.parseInt(value);

String area = value.substring(0, 3);
String group = value.substring(3,5);
String serial = value.substring(5);

setValue(new SocialSecurity(area, group, serial));
}
}
}
}


In XML

<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer" id="customEditorConfigurer">

<property name="customEditors">
<map>
<entry key="com.blogspot.sourie.SocialSecurity">
<bean class="com.blogspot.sourie.SocialSecurityPropertyEditor">
</bean>
<entry key="com.blogspot.sourie.PhoneNumber">
<bean class="com.blogspot.sourie.PhoneNumberPropertyEditor">
</bean>
</entry></entry></map>
</property>

</bean>

<bean class="com.blogspot.sourie.Person" id="phonePerson">
<property name="phoneNumber" value="111 222 3333"></property>
</bean>

1 comments

Zune (Microsoft IPod)

So after nights of searching for an Android tablet, I gave up and got a Zune instead. I just want something to write applications. But Android tablets ratings are either too low or the tablets are too expensive. The Microsoft's Zune, on the other hand, is just right. The ratings are great. The price is reasonable, arguably the cheapest of its kind. I really don't understand why it is not as famous as the IPod. Yes, it doesn't look as nice, it doesn't have a camera, and it doesn't have that many applications. But again, as I said, I just wanted something that I can write application for with wifi access. Overall, I think it's a good investment, especially now that Nokia just signed up with Microsoft.

0 comments

I Hate Networking!

Networking is the idea of creating a support system to share information and services (according to dictionary.com). The concept is great. But people are getting away from that concept. Now, people build their networks because it increases their chances of finding a job or builds their business.

But think for a moment. Suppose, you and your classmate both went for the same job. You have better work experience or better grade, but because your classmate knows someone who works in the company, he/she will get the job. To take it to another level, suppose you're from a low income family and you've been going to school full time and working part time. Therefore, you have no time to socialize (i.e. build your network). Should you be penalized for your situation? Should that rich kid who has more time to socialize because her/his parent pays for his college get the job that you're more qualified for? If you support networking in the sense that I just stated in the above paragraph, then let me answer that for you, yes! That's exactly what you're doing when you build your network so they can help you find job by giving you recommendation! You're building the society that, instead of basing it on talent, you're basing it on who you know.

I don't care if that's how the world is run. I will not let society change something that is clearly wrong into something that's acceptable. It's not. And I refuse to support such system.

On the other hand, when you go out, truly meet people. Don't think about how that person will benefit you, because he/she will. When you keep an open mind, you can always learn from someone else. Even with an enemy, you'll learn things about yourself and your enemy that could make you a better person. So next time, when you meet someone, respect the person for who he/she is. Make a real friend, and stop networking.

0 comments

Android SQLite3

Just notes for myself, hopefully I remember to clean this up.

1. Accessing the database
a. Navigate to tools folder of android sdk android-sdk-windows/tools
b. type adb shell (to access Android's shell)
c. type cd data/data/[package name]/databases
d. type sqlite3 [your database name]

SQL Commands
.tables - display all the table names
.schema [table name] - display create statement for the table

0 comments

Icons: My Tam

Since I somewhat got released from my first job, I requested a graphic challenge. This is the first round. Theme: RED


Credit appreciated.

I like My Tam ever since I heard Cay Dan Sinh Vien. She's a beautiful and charismatic singer. But more than that, I love her more for sharing her wealth by raising money to help low income students go to school. Here's a picture from one of her charity function.



Isn't she a cutie? <3