At line 8 changed 2 lines. |
In the context of [AppFuse], this is called a Manager class. It's main responsibility to act as a bridge between the persistence (DAO) layer and the |
web layer. The [Business Delegate|http://java.sun.com/blueprints/corej2eepatterns/Patterns/BusinessDelegate.html] pattern from Sun says that these objects are useful for de-coupling your presentation layer from your database layer (i.e. for Swing apps). Managers should also be where you put any business logic for your application. |
No contexto do [AppFuse], isto é chamado de classe Manager(Gerente). Sua responsabilidade principal é agir como uma ponte entre a camada de persistência(DAO) e a camada web. O padrão [Business Delegate|http://java.sun.com/blueprints/corej2eepatterns/Patterns/BusinessDelegate.html] da Sun dita que estes objetos são úteis para desacoplar a camada de apresentação da camada de dados (i.e. para aplicações Swing). Gerentes(Managers) devem ser colocados onde lógicas de negócio são necessárias. |
At line 11 changed 1 line. |
;:%%(color: blue)''I will tell you how I do stuff in the __Real World__ in text like this.''%% |
;:%%(color: blue)''Vou dizer a vocês como faço as coisas no __Mundo Real__ em textos como este.''%% |
At line 13 changed 1 line. |
Let's get started by creating a new ManagerTest and Manager in AppFuse's architecture. |
Vamos começar criando novas classes ManagerTest e Manager na arquitetura AppFuse. |
At line 15 changed 5 lines. |
!Table of Contents |
* [1] Create a new ManagerTest to run JUnit tests on the Manager |
* [2] Create a new Manager to talk to the DAO |
* [3] Configure Spring for this Manager and Transactions |
* [4] Run the ManagerTest |
!Tabela de Conteúdo |
* [1] Criar um novo ManagerTest para rodar testes JUnit no Manager |
* [2] Criar um novo Manager para conversar com o DAO |
* [3] Configurar o Spring para este Manager e as Transações |
* [4] Rodar o ManagerTest |
At line 21 changed 2 lines. |
!!Create a new ManagerTest to run JUnit tests on the Manager [#1] |
In [Part I|CreateDAO], we created a Person object and PersonDao - so let's continue developing this entity. First, let's create a JUnit test for the PersonManager. Create PersonManagerTest in the test/service/**/service directory. We'll want to test the same basic methods (get, save, remove) that our DAO has. |
!!Criar um novo ManagerTest para rodar testes JUnit no Manager [#1] |
Na [Parte I|CreateDAO_pt], criamos o objeto Person e o PersonDao - então vamos continuar a desenvolver esta entidade. Primeiramente, vamos criar o teste JUnit para o PersonManager. Crie a classe PersonManagerTest no diretório test/service/**/service. Queremos testar os mesmos métodos básicos (get, save, remove) que o nosso DAO tem. |
At line 24 changed 1 line. |
;:''This may seem redundant (why all the tests!), but these tests are GREAT to have 6 months down the road.'' |
;:''Isto pode parecer redundante (o porquê de todos estes testes!), mas estes testes são EXCELENTES para ter depois de 6 meses de estrada.'' |
At line 26 changed 1 line. |
This class should extend [BaseManagerTestCase|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/service/BaseManagerTestCase.java.html], which already exists in the ''service'' package. The parent class (BaseManagerTestCase) serves the same functionality as the BaseDaoTestCase - to load a properties file that has the same name as your *Test.class, as well as to initialize Spring's ApplicationContext. |
Esta classe deve estender [BaseManagerTestCase|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/service/BaseManagerTestCase.java.html], que já existe no pacote ''service''. A classe pai (BaseManagerTestCase) serve para o mesmo propósito da classe BaseDaoTestCase - carregar um arquivo .properties que possui o mesmo nome de sua classe *Test, assim como inicializar o ApplicationContext do Spring. |
At line 28 changed 1 line. |
;:%%(color: blue)''I usually copy (open → save as) an existing test (i.e. UserManagerTest.java) and find/replace [[Uu]ser with [[Pp]erson, or whatever the name of my object is.''%% |
;:%%(color: blue)''Usualmente eu copio (open → save as) um teste existente (i.e. UserManagerTest.java) e utilizando ctrl+f para encontrar/substituir [[Uu]ser com [[Pp]erson, ou qualquer que seja o nome do meu objeto.''%% |
At line 30 changed 1 line. |
The code below is what we need for a basic JUnit test of our Managers. The code below simply creates and destroys the PersonManager. The "ctx" object is initialized in the [BaseManagerTestCase|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/service/BaseManagerTestCase.java.html] class. |
O código abaixo é o que precisamos para um teste JUnit básico para nossos Managers. Diferente do DaoTest, este teste utiliza [jMock|http://jmock.org] para isolar o Manager de suas dependências e fazer um teste unitário ''verdadeiro''. |
At line 36 changed 3 lines. |
import org.apache.commons.logging.Log; |
import org.apache.commons.logging.LogFactory; |
|
import java.util.List; |
import java.util.ArrayList; |
import org.appfuse.dao.PersonDao; |
At line 39 added 1 line. |
import org.appfuse.service.impl.PersonManagerImpl; |
At line 41 added 2 lines. |
import org.jmock.Mock; |
import org.springframework.orm.ObjectRetrievalFailureException; |
At line 45 added 4 lines. |
private final String personId = "1"; |
private PersonManager personManager = new PersonManagerImpl(); |
private Mock personDao = null; |
private Person person = null; |
At line 44 changed 6 lines. |
private Person person; |
private PersonManager mgr = null; |
private Log log = LogFactory.getLog(PersonManagerTest.class); |
|
protected void setUp() { |
mgr = (PersonManager) ctx.getBean("personManager"); |
protected void setUp() throws Exception{ |
super.setUp(); |
personDao = new Mock(PersonDao.class); |
personManager.setPersonDao((PersonDao) personDao.proxy()); |
At line 53 changed 1 line. |
mgr = null; |
super.tearDown(); |
personManager = null; |
At line 55 changed 4 lines. |
|
public static void main(String[] args) { |
junit.textui.TestRunner.run(PersonManagerTest.class); |
} |
|