Entities and entity beans

EJB 3 entities and EJB 2.x entity beans are different beasts from the other kinds of Enterprise JavaBeans, session beans, and message-driven beans. Entities represent data and operations related to finding entities by various criteria, modifying the data represented by a given entity bean instance, and creating and deleting data in other words, persistent objects. The majority of entity beans written by corporate developers have traditionally had no functionality beyond what's generated by the...

Listing Letting ejbPostCreate create a Timer

public void setEntityContext EntityContext ctx this.ctx ctx public void ejbPostCreate String username, String password ctx.getTimerService .createTimer BunchOfConstants.TWO_MONTHS, password expired We know the UserBean class should be abstract, implement the javax.ejb.Enti-tyBean interface, and so forth, in order to be a valid entity bean implementation, according to the EJB 2.1 specification. Consider writing some contract tests for checking such things for our entity beans. Leaving that...

Anatomy of a typical framework

Looking at component-based frameworks, we can identify certain patterns emerging from their respective architectures. And looking at these patterns, we can already say a lot about how receptive these frameworks are for unit-testing and, specifically, test-driven development. First of all, the word component in component-based refers to the existence of UI components. These are widgets implemented as regular Java classes, often extending the selected framework's built-in base classes or...

Testdriving Spring controllers

The Spring Framework realizes the C in MVC through different types of Controller classes. They're similar to Servlets but somewhat more abstract. For one, Spring controllers are not responsible for dispatching the request to the view directly. Instead, they indicate to the surrounding framework what the framework should do by returning a ModelAndView object. To clarify this a bit, let's look at some code. Listing 5.11 Example of a Spring controller public class SampleController implements...

Essential testing concepts

French political scientist Alexis de Tocqueville has said that the last thing a political party abandons is its vocabulary.5 You could say the same thing about software engineers. Vocabulary is not just words, though. A shared vocabulary helps us communicate with our fellow engineers about our work, and a good vocabulary helps us understand the purpose and intent of the concepts it describes. There are a few basic concepts related to the unit tests that we write as part of test-driven...

identify social leaders

Every group has informal, social leaders. Although the social leaders are often people who others look up to and rely on for advice, they are not necessarily the most experienced or the most skilled individuals in the group. People who have a natural tendency to motivate others and generally keep the pack together are often great social leaders. By giving a suitable role to such a social leader, we essentially create a deputy change agent and give them a megaphone to run with. Getting a social...

Devent queue API

Let's see what kinds of approaches we use in listing 11.2 to skin the weblog. The first action method, subscribeForAnnouncements, O performs a Java Naming and Directory Interface JNDI lookup to a remote application server where the system under test is running. This approach exploits the system's architecture that happens to make the internal API easily accessible also from tests running in a remote Java Virtual Machine JVM . The second method, postNewBlogEntry, on the other hand, C accesses an...

Listing Using MockEJB and EasyMock to mock the JNDI tree

import javax.naming. import org.junit. import static org.junit.Assert. import static org.easymock.EasyMock. public class PricingServiceBeanTest Before public void setUp throws Exception MockContextFactory.setAsInitial C Reset everything after each test public void discountBasedOnVolumeOfPastPurchases throws Exception Account account new MockAccount Product product new Product , 10000 price 100 DiscountService ds createMock DiscountService. replay ds

Lets not forget to refactor

We haven't yet refactored anything, even though we've written a lot of code. The reason is that we haven't spotted anything that needs refactoring. Is there still nothing to refactor Probably not, because we didn't add any code, right Wrong. Even though we didn't add any production code, we added test code, and that's code just like any other and we know better than to let our test code rot and get us into serious trouble later on. Let's see what we could do about our test code by first...

Listing DoFixture from listing returning two different SetUpFixtures

public class ReviewsDoFixture extends fit.DoFixture public Fixture setupBookData throws Exception return new BookSetupFixture public Fixture setupReviewData throws Exception return new ReviewSetupFixture The next table after our two initial SetUpFixture tables is closer to a regular ActionFixture again, with two actions mapping to methods goToFrontPage and textPresent String . Here's the corresponding Java code for reference public class ReviewsDoFixture extends fit.DoFixture public boolean...

Listing DoFixture returning SetUpFixture to handle remainder of table

public class ReviewsDoFixture extends fit.DoFixture public Fixture setupBookData throws Exception return new BookSetupFixture This effectively means that the remaining four rows of the table are handled by the BookSetupFixture a subclass of SetUpFixture that we'll talk about in the next section that we passed back to Fit. Similarly, the first action on the next table, setup review data, maps to another method on the ReviewsDoFixture class because the scope of the BookSetupFixture doesn't span...

Listing Valid login test rewritten to specify use of AuthenticationService

public void throws Exception final String validUsername validuser final String validPassword validpassword final FakeAuthenticationService authenticator new FakeAuthenticationService authenticator.addUser validUsername, validPassword LoginServlet servlet new LoginServlet Override protected AuthenticationService getAuthenticationService return authenticator

Reducing pain with Springs JdbcTemplate

Dao Template

The Spring Framework includes a facility named JdbcTemplate, which purports to simplify data-access code to only the essential. The fundamental idea behind the implementation is that most raw JDBC code follows the same pattern with certain boilerplate code always present nested try-catch structures with conditional close calls for making sure all resources are released is a prime example. In practice, JdbcTemplate employs something similar to the well-known Template Method design pattern2 and...