> 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/mock.md).

# Mock

En muchas ocasiones necesitamos realizar operaciones en donde asignamos valores a los entity para ejecutar el test con esos datos.

Por ejemplo ingresamos datos al entity Bodega y ejecutamos la operaciòn save() en la colecciòn.

* Al final podemos eliminarlo despues de realizar el test.
* O lo dejamos para otras pruebas y al final lo eliminamos cuando terminamos el proceso.

![](/files/-Lc1WoXdncfMdsWRYH6N)

## Ejercicio:

Agregar el test del metodo save() a BodegaTest.java

**Crear un Mock**

```java
            Bodega bodega = new Bodega();
            bodega.setIdbodega("bodega-test");
            bodega.setDireccion("Panama");
            bodega.setTelefono("(507)");
            //User info es una clase que usa el framework para guardar referencias
            //de usuarios
            List<UserInfo> list = new ArrayList<>();
            bodega.setUserInfo(list);
            bodega.setActivo("si");
```

**Implementamos**&#x20;

* **assertEquals()** para verificar si logro guardarlo.

```java
unitTest.assertEquals("save()", true,bodegaRepository.save(bodega));
```

**Mètodo**

```java
 @Test
    private void save() {
        try {
            //Mock
            Bodega bodega = new Bodega();
            bodega.setIdbodega("bodega-test");
            bodega.setDireccion("Panama");
            bodega.setTelefono("(507)");
            //User info es una clase que usa el framework para guardar referencias
            //de usuarios
            List<UserInfo> list = new ArrayList<>();
            bodega.setUserInfo(list);
            bodega.setActivo("si");

            unitTest.assertEquals(nameOfMethod(), true,bodegaRepository.save(bodega));
        } catch (Exception e) {
            System.out.println(nameOfMethod()+" " + e.getLocalizedMessage());
        }

    }
```

## 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);
        save();
        findAll();


    }

    @Test
    private void save() {
        try {
            //Mock
            Bodega bodega = new Bodega();
            bodega.setIdbodega("bodega-test");
            bodega.setDireccion("Panama");
            bodega.setTelefono("(507)");
            //User info es una clase que usa el framework para guardar referencias
            //de usuarios
            List<UserInfo> list = new ArrayList<>();
            bodega.setUserInfo(list);
            bodega.setActivo("si");

            unitTest.assertEquals(nameOfMethod(), true,bodegaRepository.save(bodega));
        } catch (Exception e) {
            System.out.println(nameOfMethod()+" " + e.getLocalizedMessage());
        }

    }

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

    }

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

## Al ejecutar el Test

**Resumen**

![](/files/-Lc1WoXin8UkmkXaAJcE)

Clases List: Pasaron todos los test.

![](/files/-Lc1WoXkbkvp5bFViDSk)

**Metodos fueron exitosos**

![](/files/-Lc1WoXmR21LbKDuHQNF)

## **Si ejecutamos nuevamente el test**

El resumen indica que hay 1 error

![](/files/-Lc1WoXoN4aSYBigrYUP)

**Clases List**

Se observa que BodegaTest tiene dos errores

![](/files/-Lc1WoXqNBv6K6F_H66y)

Al verificar la clase BodegaTest

Se observa que el metodo que fallo fue el metodo save()

![](/files/-Lc1WoXs8nsUdLM4-Am6)

Para tener màs detalles sobre los mensajes de error podemos implementar UnitView\.java


---

# 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/mock.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.
