Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Quick Start

Qaiser Abbasi edited this page Jan 20, 2015 · 1 revision

This section makes you quickly comfortable with the concepts and the API design of CDI BeanTesting.

Requirements

Getting Binaries

In order to add CDI BeanTesting to a Maven-based project, put the following snippets to your pom.xml file

Novatec Maven Repository

    <repository>
       <id>Novatec</id>
       <name>Novatec Repository</name>
       <url>http://repository.novatec-gmbh.de/content/repositories/novatec</url>
    </repository>

CDI BeanTesting Dependency

     <dependency>
      <groupId>info.novatec</groupId>
      <artifactId>bean-test</artifactId>
      <version>{currentVersion}</version>
      <scope>test</scope>
    </dependency>

Alternatively, binaries can also be found at http://repository.novatec-gmbh.de/content/repositories/novatec/info/novatec/bean-test/

Configuration

There are just a few things which need be to configured.

  1. Create an empty beans.xml file (marker file for the CDI Bean container) under 'src/test/resources/META-INF'.
  2. Created a persistence.xml file in 'src/test/resources/META-INF'.
  3. Create a persistence unit called "beanTestPU" in the created persistence.xml file.

Write a CDI Beantest

Note: Import statements are intentionally missing.

First things first. Lets create a simple component:

public class ServiceOfWisdom {
  @Inject Instance<String> answer;

  public String getAnswer() {
     return answer.get();
  }
}

public class AnswerProducer {
  @Produces final static String answer = "42"; 
}

Now create a JUnit test class for the service:

public class TestServiceOfWisdom extends BaseBeanTest {
  
  @Test
  public void test_if_service_is_successfully_injected() {
    ServiceOfWisdom service = getBean(ServiceOfWisdom.class);
    assertThat(service, is(notNullValue()));
    assertThat(service.getAnswer(), is("42"));
  }
}

Run the test

You can either run the test as commonly practised in your favourite IDE as a normal JUnit test or via Maven.

Debug the test

CDI BeanTesting provides straightforward debugging functionality.

Clone this wiki locally