...

/

Solution Review: Dependency Injection Using CDI

Solution Review: Dependency Injection Using CDI

Review the solution for the “Dependency Injection Using CDI” exercise.

We'll cover the following...

Solution

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
	<artifactId>junit5-file</artifactId>
	<version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <junit.platform.version>1.2.0</junit.platform.version>
        <junit.jupiter.version>5.2.0</junit.jupiter.version>
        <hamcrest.version>1.3</hamcrest.version>
        <mockito.version>2.7.19</mockito.version>
        <random.beans.version>3.9.0</random.beans.version>

        <maven.surefire.plugin.version>2.22.0</maven.surefire.plugin.version>
        <maven.jar.plugin.version>3.0.2</maven.jar.plugin.version>
        <maven.javadoc.plugin.version>3.0.0-M1</maven.javadoc.plugin.version>
        <maven.coveralls.plugin.version>4.3.0</maven.coveralls.plugin.version>
        <maven.jacoco.plugin.version>0.7.9</maven.jacoco.plugin.version>
        <maven.source.plugin.version>3.0.0</maven.source.plugin.version>
    </properties>

    <build>
        <plugins>
            <!-- test runner -->
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven.surefire.plugin.version}</version>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>${junit.platform.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>${junit.jupiter.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

            
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.jupiter.version}</version>
        </dependency>
        <dependency>
            <groupId>io.github.benas</groupId>
            <artifactId>random-beans</artifactId>
            <version>${random.beans.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>
        <dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-params</artifactId>
			<version>${junit.jupiter.version}</version>
			<scope>test</scope>
		</dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>${junit.platform.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
        </dependency> 
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-runner</artifactId>
            <version>${junit.platform.version}</version>
            <scope>test</scope>
        </dependency>
                <!-- https://mvnrepository.com/artifact/jakarta.inject/jakarta.inject-api -->
        <dependency>
    <groupId>org.jboss.weld.se</groupId>
    <artifactId>weld-se</artifactId>
    <version>2.3.4.Final</version>
</dependency>
        <dependency>
            <groupId>jakarta.inject</groupId>
            <artifactId>jakarta.inject-api</artifactId>
            <version>2.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
            <version>${hamcrest.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>${mockito.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
  <groupId>org.jboss.weld</groupId>
  <artifactId>weld-core</artifactId>
  <version>1.1.8.Final</version>
  <exclusions>
    <exclusion>
      <artifactId>jsr250-api</artifactId>
      <groupId>javax.annotation</groupId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>org.jboss.weld.servlet</groupId>
  <artifactId>weld-servlet-core</artifactId>
  <version>1.1.8.Final</version>
</dependency>
	<dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>
    </dependencies>

</project>
Dependency injection using CDI

Explanation

  • We’ve created the UserService class and defined the three find(), createOrUpdateUser()
...