# Testing

**Requerimientos:**

* ejbjmoordb
* jmoordbUnit
* Base de datos: MongoDB

Muchas ocasiones deseamos hacer testing de las bases de datos sin afectar las de producción

podemos utilizar el método repository.setDatabase(nombre-base-datos) para indicar la base de datos con la que deseamos hacer las pruebas, si no existe se creara en tiempo de ejecución.

Debemos establecerlo en el método init()

```java
  rolRepository.setDatabase("transporte_test");
```

## Ejemplo:

Tenemos una base de datos llamada transporte y deseamos hacer los test sobre una base de datos que crearemos dinamicamente llamada transporte\_test

![](/files/-Lc1WseyaU2ugroUtnaj)

### Código

```java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.avbravo.transportetest.test;

import com.avbravo.avbravoutils.JsfUtil;
import com.avbravo.ejbjmoordb.pojos.UserInfo;
import com.avbravo.jmoordbunit.anotation.Test;
import com.avbravo.jmoordbunit.test.UnitTest;
import com.avbravo.transporteejb.entity.Rol;
import com.avbravo.transporteejb.repository.RolRepository;
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;
import org.bson.Document;

/**
 *
 * @author avbravo
 */
@Startup
@Singleton
@DependsOn("ConductorTest")
public class RolTest implements ITest {
    @Inject
    UnitTest unitTest;
    @Inject
    RolRepository rolRepository;
     @PostConstruct
    void init() {
        unitTest.start(RolTest.class);
        rolRepository.setDatabase("transporte_test");
        findAll();
        save();
      delete();

    }

      @PreDestroy
    public void destroy() {
        unitTest.end(RolTest.class);

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

    @Test
    private void save(){
        try {
            Rol rol = new Rol();
            rol.setIdrol("TEST");
            rol.setRol("ROL TESTING");
            rol.setActivo("si");
            List<UserInfo> userInfoList = new ArrayList<>();
            userInfoList.add(new UserInfo("xx", "avbravo", JsfUtil.getFechaActual(), "save()"));
            rol.setUserInfo(userInfoList);
            unitTest.assertTrue("save()", rolRepository.save(rol));
        } catch (Exception e) {
            unitTest.fail("save()", rolRepository.getException().toString());
            System.out.println(nameOfMethod()+" "+e.getLocalizedMessage());
        }
    }

    @Test
    private void delete(){
        unitTest.assertTrue(nameOfMethod(), rolRepository.delete(new Document("idrol","TEST")));
    }




}
```


---

# Agent Instructions: 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:

```
GET https://avbravo-2.gitbook.io/jmoordbunit/testing-de-bases-de-datos-dinamicas/testing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
