Java JSF支持Bean单元测试
我有一个支持bean,叫做PeopleListBean。目的很简单:从存储库返回人员列表Java JSF支持Bean单元测试,java,jsf,junit,mockito,Java,Jsf,Junit,Mockito,我有一个支持bean,叫做PeopleListBean。目的很简单:从存储库返回人员列表 public class PeopleListBean { @Autowired private PersonRepository personRepository; private List<Person> people; @PostConstruct private void initializeBean() { thi
public class PeopleListBean {
@Autowired
private PersonRepository personRepository;
private List<Person> people;
@PostConstruct
private void initializeBean() {
this.people = loadPeople();
}
public List<User> getPeople() {
return this.people;
}
private List<Person> loadPeople() {
return personRepository.getPeople();
}
}
公共类PeopleListBean{
@自动连线
个人知识库;
私人名单;
@施工后
private void initializeBean(){
this.people=loadPeople();
}
公共列表getPeople(){
把这个还给我;
}
私人列表loadPeople(){
返回personRepository.getPeople();
}
}
我想使用Junit和Mockito为这个bean创建一个单元测试。下面的测试类示例:
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.example.PersonRepository;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/test-application-context.xml" })
public class PeopleListBeanTest {
@Autowired
private PeopleListBean peopleListBean;
@Autowired
private PersonRepository mockPersonRepository;
@Before
public void init() {
reset(mockPersonRepository);
}
@Test
public void canListPeople() {
List<Person> people = getDummyList();
when(mockPersonRepository.getPeople().thenReturn(people);
assertTrue(peopleListBean.getPeople().size() == people.size());
}
}
import static org.junit.Assert.assertTrue;
导入静态org.mockito.mockito.reset;
导入静态org.mockito.mockito.when;
导入java.util.ArrayList;
导入java.util.List;
导入org.junit.Before;
导入org.junit.Test;
导入org.junit.runner.RunWith;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.test.context.ContextConfiguration;
导入org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
导入com.example.PersonRepository;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(位置={“classpath:/test application context.xml”})
公共类PeopleListBeanTest{
@自动连线
私人PeopleListBean PeopleListBean;
@自动连线
私有PersonRepository mockPersonRepository;
@以前
公共void init(){
重置(mockPersonRepository);
}
@试验
公众人士({
List people=getDummyList();
when(mockPersonRepository.getPeople()),然后return(people);
assertTrue(peopleListBean.getPeople().size()==people.size());
}
}
我的问题是,何时/如何模拟存储库,因为加载发生在initializeBean方法(@PostConstruct)中。因此,在构建类之后,在我实际模拟该方法之前调用“getPeople”方法,从而导致断言不匹配
我非常感谢您的帮助/指导!使用JUnit的注释
因此,您的代码如下所示:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/test-application-context.xml" })
public class PeopleListBeanTest {
@Autowired
private PeopleListBean peopleListBean;
@Autowired
private PersonRepository mockPersonRepository;
@BeforeClass
public static void initialise() {
}
// .
// .
// .
}