This is a simple demo to demonstrate how to test a jsf scoped bean in Spring Boot context using JoinsFaces.
This code is inspired by some commercial project commits of @DEXRAY and updated by @larmic and @renepa
Dependency is available in maven central repository. Just add to your pom.xml
<dependency>
<groupId>de.larmic</groupId>
<artifactId>joinfaces-bean-test</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
Version | Spring Boot | JoinFaces |
---|---|---|
<= 0.3 | 1.5.x | 2.3.x |
0.4 | 2.0.x | 3.0.0.RC1 |
0.6 | 2.0.x | 3.0.x |
0.7 | 2.0.x | 3.1.x |
>= 0.8 | 2.0.x | 3.2.x |
1.0 | 2.1.x | 4.x.x |
@Named
@RequestScoped
public class ContractsBean {
private String customerNumber;
@PostConstruct
public void init() {
customerNumber = FacesContext.getCurrentInstance()
.getExternalContext()
.getRequestParameterMap()
.get("customerNumber");
}
public String getCustomerNumber() {
return customerNumber;
}
}
This simple bean reads url query parameter customerNumber. Using JoinFaces and SpringBoot this bean is not testable by default injection into a test class.
This demo allows following integration test:
@RunWith(SpringRunner.class)
@ContextConfiguration(initializers = {FacesContextMockApplicationContextInitializer.class})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
public class ContractsBeanTest {
@Autowired
private ApplicationContext context;
@Test
public void testCreateRequestScopedBeanWithCustomerNumberIsSet() {
final ContractsBean bean = new JsfSpringBeanBuilder(context)
.withExternalParameter("customerNumber", "unit-test-customer-number")
.build(ContractsBean.class);
assertThat(bean.getCustomerNumber()).isEqualTo("unit-test-customer-number");
}
@Test
public void testCreateRequestScopedBeanWithCustomerNumberIsNull() {
final ContractsBean bean = new JsfSpringBeanBuilder(context)
.build(ContractsBean.class);
assertThat(bean.getCustomerNumber()).isNull();
}
}