https://github.com/larmic/joinfaces-bean-test
Demonstrates a way to create an JSF bean integration test
https://github.com/larmic/joinfaces-bean-test
butterfaces integration-testing joinfaces spring-boot unit-testing unittest
Last synced: 3 months ago
JSON representation
Demonstrates a way to create an JSF bean integration test
- Host: GitHub
- URL: https://github.com/larmic/joinfaces-bean-test
- Owner: larmic
- License: apache-2.0
- Created: 2017-03-03T10:04:59.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2022-03-31T18:38:46.000Z (about 4 years ago)
- Last Synced: 2025-07-27T03:36:52.997Z (9 months ago)
- Topics: butterfaces, integration-testing, joinfaces, spring-boot, unit-testing, unittest
- Language: Java
- Size: 46.9 KB
- Stars: 10
- Watchers: 5
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# JSF Bean testing using JoinFaces and SpringBoot
[](https://travis-ci.org/larmic/joinfaces-bean-test)
[](https://coveralls.io/github/larmic/joinfaces-bean-test?branch=master)
[](https://opensource.org/licenses/Apache-2.0)
This is a simple demo to demonstrate how to test a jsf scoped bean in Spring Boot context using [JoinsFaces](https://github.com/joinfaces/joinfaces).
This code is inspired by some commercial project commits of @DEXRAY and updated by @larmic and @renepa
## Usage
Dependency is available in maven central repository. Just add to your pom.xml
```xml
de.larmic
joinfaces-bean-test
1.0
test
```
## Compatibility list
| 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
## Example
```java
@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:
```java
@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();
}
}
```