joinfaces-bean-test

Demonstrates a way to create an JSF bean integration test

License

License

GroupId

GroupId

de.larmic
ArtifactId

ArtifactId

joinfaces-bean-test
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

joinfaces-bean-test
Demonstrates a way to create an JSF bean integration test
Project URL

Project URL

https://github.com/larmic/joinfaces-bean-test
Source Code Management

Source Code Management

https://github.com/larmic/joinfaces-bean-test

Download joinfaces-bean-test

How to add to project

<!-- https://jarcasting.com/artifacts/de.larmic/joinfaces-bean-test/ -->
<dependency>
    <groupId>de.larmic</groupId>
    <artifactId>joinfaces-bean-test</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/de.larmic/joinfaces-bean-test/
implementation 'de.larmic:joinfaces-bean-test:1.0.0'
// https://jarcasting.com/artifacts/de.larmic/joinfaces-bean-test/
implementation ("de.larmic:joinfaces-bean-test:1.0.0")
'de.larmic:joinfaces-bean-test:jar:1.0.0'
<dependency org="de.larmic" name="joinfaces-bean-test" rev="1.0.0">
  <artifact name="joinfaces-bean-test" type="jar" />
</dependency>
@Grapes(
@Grab(group='de.larmic', module='joinfaces-bean-test', version='1.0.0')
)
libraryDependencies += "de.larmic" % "joinfaces-bean-test" % "1.0.0"
[de.larmic/joinfaces-bean-test "1.0.0"]

Dependencies

provided (4)

Group / Artifact Type Version
org.joinfaces : jsf-spring-boot-starter jar 4.0.1
org.springframework.boot : spring-boot-starter-web jar 2.1.1.RELEASE
javax.enterprise : cdi-api jar 2.0
org.springframework.boot : spring-boot-starter-test jar 2.1.1.RELEASE

Project Modules

There are no modules declared in this project.

JSF Bean testing using JoinFaces and SpringBoot

Build Status Coverage Status License

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

Usage

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>

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

@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();
    }
}

Versions

Version
1.0.0
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.2.ALPHA5