简体   繁体   中英

How to test a Controller and Model in a JSF Project with jUnit?

i don't know exactly how to write tests for these following Classes especially for the Controller and Model. Is it to possible to test with jUnit ? I heard from Selenium but first i would test with jUnit. Thanks for ur help and best regards.

Controller.class:

 import factory.InfoMessageFactory; import entity.Product; import java.io.Serializable; import java.util.List; import javax.annotation.PostConstruct; import javax.enterprise.context.SessionScoped; import javax.faces.application.FacesMessage; import javax.faces.context.FacesContext; import javax.inject.Inject; import javax.inject.Named; import model.ProductModel; import project.Konstanten; @Named(value = "ProductController") @SessionScoped public class ProductController implements Serializable { private Product product; @Inject private ProductModel model; @PostConstruct public void init() { this.product = new Product(); } public String addProduct() { this.model.newProduct(this.product); } public Product getProduct() { return product; } public void setProdukt(Product product) { this.product = product; } public List<Product> getProducts() { return this.model.getProducts(); } } 

Model.class

 package model; import ejb.DB; import entity.Product; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import javax.enterprise.context.Dependent; import javax.inject.Inject; @Dependent public class ProductModel implements Serializable{ @Inject private DB db; public boolean addProduct(Product p){ try{ db.persist(p); }catch(Exception e){ System.out.println("Blablabla"); return false; } return true; } } 

And DB.class

 @Stateless public class DB { @Inject @RealClass private EntityManager em; public void persist(Object object) { em.persist(object); } 

In the ProductController , there is really not much to test.. unless there is more logic that you did not post.

For testing the ProductModel , or any service-like class having the DB dependency i would suggest adding a project dependency to one of the mocking frameworks (i suggest Mockito as it is the most mature of them all).

For the addProducts method you could end up with following tests:

import static org.mockito.Mockito.*;

import org.junit.Test;
import org.mockito.MockitoAnnotations;

public class ProductModelTest{

@Mock
private DB dbMock;

@InjectMocks
private ProdcutModel = new ProductModel();

@Before
public void init(){
   MockitoAnnotations.iniMocks(this);
}

@Test
public void shouldReturnTrue_whenEntityPersisted(){
    doNothing().when(dbMock).persist(any(Product.class));

    boolean result = productModel.addProduct(new Product());

    assertTrue(result);
}

@Test
    public void shouldReturnFalse_whenEntityPersisted(){
        doThrow(RuntimeException.class).when(dbMock).persist(any(Product.class));

        boolean result = productModel.addProduct(new Product());

        assertFalse(result);
}
}

Regarding the DB-like repository classes.. i normally do not unit-test them. IF so i run integration tests on them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM