> For the complete documentation index, see [llms.txt](https://avbravo-2.gitbook.io/jmoordbunit/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://avbravo-2.gitbook.io/jmoordbunit/proyecto-web/crear-test.md).

# Test

Un Test con jmoordbunit es un EJB que se ejecuta al iniciar el contenedor.

## Anotaciones

![](/files/-Lc1WnxzoCZZ-OfJFvHE)

Debe contener las siguientes anotaciones:

* @Startup
* @Singleton
* @DependsOn(Name\_of*EJB\_dependiente*)
* Report(path="path\_reportes") //Opcional
* @Test (en los **métodos** que se realizara el testing)

**Detalle:**

@Startup:

* Indica que se ejecutara al iniciar la aplicaciòn en el contenedor

@Singleton

* Indicamos que sera unico

@DependsOn()

* El primero que deseamos ejecutar debe implementar  la clase @DependsOn("TestEnvironment")
* Si deseamos que se ejecute después de otro , idicamos el nombre del EJB padre @DependsOn("Padre")

@Report(path="")

* Indicamos la ruta en que se generaran los reportes de los test.

## UnitTest

Esta clase contiene todos los mètodos necesarios para ejecutar el test.

![](/files/-Lc1Wny0nXJCLlPwM9D7)

```java
@Inject
UnitTest unitTest;
```

**Generalmente necesitamos Inyectar:**

* Repository
* Services (Opcional)

![](/files/-Lc1Wny2WFNtvGawlEVY)

## M**étodos**

Usamos metodos:

* init(): Que se ejecuta al iniciar el ejb, alli colocamos los mètodos a realizar el test e inicializamos el UnitTest
* destroy(): Se ejecuta cuando de destruye el EJB.

**Ejemplo:**

```java
@PostConstruct
    void init() {
        unitTest.start(BodegaTest.class);
//       invocar metodos a realizar el test

    }

     @PreDestroy
    public void destroy() {
        unitTest.end(BodegaTest.class);
    }
```

## Test simple

**Objetivo del Test:**

* Ejecutar un test para un metodo no creado donde queremos verificar si no hay datos en una colecciòn llamada bodega.

**Metodo:**

* findAll()&#x20;

Usamos el mètodo unitTest.assertNotEquals("metodo",resultado\_esperado, expresion\_evaluar);

El verificara que el resultado no sea igual a cero y contara internamente el contador del evento.

```java
unitTest.assertNotEquals("findAll", 0, bodegaRepository.findAll().size());
```

**Agregarlo al init()**

```java
@PostConstruct
void init() {
     unitTest.start(BodegaTest.class);
     findAll();
     save();
}
```

## BodegaTest.java

```java
import com.avbravo.ejbjmoordb.pojos.UserInfo;
import com.avbravo.jmoordbunit.anotation.Report;
import com.avbravo.jmoordbunit.anotation.Test;
import com.avbravo.jmoordbunit.test.UnitTest;
import com.avbravo.microtestingejb.entity.Bodega;
import com.avbravo.microtestingejb.repository.BodegaRepository;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.DependsOn;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;

/**
 *
 * @author avbravo
 */
@Startup
@Singleton
@DependsOn("TestEnvironment")
@Test
@Report(path = "/home/avbravo/Descargas/")
public class BodegaTest {

    @Inject
    UnitTest unitTest;
    @Inject
    BodegaRepository bodegaRepository;

    @PostConstruct
    void init() {
        unitTest.start(BodegaTest.class);
        findAll();


    }


    @Test
    private void findAll() {
        unitTest.assertNotEquals(nameOfMethod(), 0, bodegaRepository.findAll().size());

    }

    @PreDestroy
    public void destroy() {
        unitTest.end(BodegaTest.class);
    }
}
```

Cuando se ejecuta el proyecto de Test:

Podemos ver varios informes:

* Resumen de los test.
* Clases List: Nos muestra las clases y el total de resultados de cada operación
* Podemos observar que se ejecuto en solo test sobre un método y el resultado fue exitoso, es decir la colección tiene documentos no esta vacía.

**Resumen:**

![](/files/-Lc1Wny48dTblRIaGOyL)

**Clases List:**

![](/files/-Lc1Wny6gBzstUjCr31B)

Podemos ver el detalle de los metodos ejecutados:

![](/files/-Lc1Wny8dYHh5JybLf3Z)

En este caso el test fallo, debido a que no tenemos documentos en la colecciòn.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://avbravo-2.gitbook.io/jmoordbunit/proyecto-web/crear-test.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
