Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct JUnit Repository Test, update to JUnit 5 #32

Open
JuKu opened this issue Aug 4, 2018 · 0 comments
Open

Correct JUnit Repository Test, update to JUnit 5 #32

JuKu opened this issue Aug 4, 2018 · 0 comments
Assignees
Labels
backlog enhancement New feature or request Priority: High type: test JUnit Tests, Integration tests and so on

Comments

@JuKu
Copy link
Collaborator

JuKu commented Aug 4, 2018

I got some help from zordan (a software architect), he corrected some things, so Spring JPA / Hibernate JUnit Tests runs correctly with JUnit 5:

Corrected pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://maven.apache.org/POM/4.0.0"

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>de.agdsn</groupId>

<artifactId>jcroft</artifactId>

<version>1.0.0</version>

<packaging>jar</packaging>

<name>JCroft</name>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.0.3.RELEASE</version>

</parent>

<properties>

<!-- Java version -->

<java.version>1.8</java.version>

<!-- JUnit version -->

<junit5.version>5.2.0</junit5.version>

<mockito.version>2.2.7</mockito.version>

<jacoco.reportPath>${project.basedir}/../target/jacoco.exec</jacoco.reportPath>

<!-- Sonar properties-->

<sonar.jacoco.itReportPath>${project.basedir}/../target/jacoco-it.exec</sonar.jacoco.itReportPath>

<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>

<sonar.language>java</sonar.language>

<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>

<!-- file encoding -->

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

</properties>

<!-- tag::security[] -->

<dependencies>

<!-- JUnit -->

<!--<dependency>-->

<!--<groupId>org.junit.jupiter</groupId>-->

<!--<artifactId>junit-jupiter-api</artifactId>-->

<!--<version>${junit5.version}</version>-->

<!--<scope>test</scope>-->

<!--</dependency>-->

<dependency>

<groupId>org.junit.jupiter</groupId>

<artifactId>junit-jupiter-engine</artifactId>

<version>${junit5.version}</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.junit.vintage</groupId>

<artifactId>junit-vintage-engine</artifactId>

<version>${junit5.version}</version>

<scope>test</scope>

</dependency>

<!--<dependency>-->

<!--<groupId>org.junit.platform</groupId>-->

<!--<artifactId>junit-platform-launcher</artifactId>-->

<!--<version>1.1.0</version>-->

<!--<scope>test</scope>-->

<!--</dependency>-->

<!--<dependency>-->

<!--<groupId>org.junit.platform</groupId>-->

<!--<artifactId>junit-platform-runner</artifactId>-->

<!--<version>1.1.0</version>-->

<!--<scope>test</scope>-->

<!--</dependency>-->

<!-- for JUnit tests -->

<dependency>

<groupId>org.mockito</groupId>

<artifactId>mockito-core</artifactId>

<version>${mockito.version}</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-jpa</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-security</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-websocket</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.security</groupId>

<artifactId>spring-security-messaging</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework.security</groupId>

<artifactId>spring-security-test</artifactId>

<scope>test</scope>

</dependency>

<!-- H2 database for junit tests only -->

<dependency>

<groupId>com.h2database</groupId>

<artifactId>h2</artifactId>

<version>1.4.197</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.apache.directory.api</groupId>

<artifactId>apache-ldap-api</artifactId>

<version>1.0.0</version>

<type>pom</type>

<exclusions>

<exclusion>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-log4j12</artifactId>

</exclusion>

<exclusion>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId>

</exclusion>

</exclusions>

</dependency>

<dependency>

<groupId>com.hazelcast</groupId>

<artifactId>hazelcast</artifactId>

</dependency>

<dependency>

<groupId>com.hazelcast</groupId>

<artifactId>hazelcast-hibernate5</artifactId>

<version>1.2.3</version>

</dependency>

<dependency>

<groupId>org.hibernate</groupId>

<artifactId>hibernate-core</artifactId>

<version>5.2.17.Final</version>

</dependency>

<dependency>

<groupId>com.hazelcast</groupId>

<artifactId>hazelcast-spring</artifactId>

</dependency>

<dependency>

<groupId>postgresql</groupId>

<artifactId>postgresql</artifactId>

<version>9.1-901-1.jdbc4</version>

</dependency>

</dependencies>

<!-- end::security[] -->

<reporting>

<plugins>

<plugin>

<groupId>org.codehaus.mojo</groupId>

<artifactId>jdepend-maven-plugin</artifactId>

<version>2.0</version>

</plugin>

</plugins>

</reporting>

<build>

<plugins>

<!-- set Java version -->

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.7.0</version>

<configuration>

<source>${java.version}</source>

<target>${java.version}</target>

</configuration>

</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-source-plugin</artifactId>

<version>2.2.1</version>

<executions>

<execution>

<id>attach-sources</id>

<goals>

<goal>jar-no-fork</goal>

</goals>

</execution>

</executions>

</plugin>

<plugin>

<artifactId>maven-surefire-plugin</artifactId>

<version>2.20</version>

<dependencies>

<dependency>

<!-- contains the engine that actually runs the Jupiter-tests -->

<groupId>org.junit.jupiter</groupId>

<artifactId>junit-jupiter-engine</artifactId>

<version>5.2.0</version>

</dependency>

</dependencies>

</plugin>

<!-- <plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-failsafe-plugin</artifactId>

<version>2.19.1</version>

<executions>

<execution>

<goals>

<goal>integration-test</goal>

<goal>verify</goal>

</goals>

<configuration>

<skipTests>${skip.tests}</skipTests>

<argLine>${argLine} -Duser.timezone=UTC -Xms256m -Xmx256m</argLine>

<includes>

<includes>**/*Test*</includes>

</includes>

</configuration>

</execution>

</executions>

</plugin> -->

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

<plugin>

<groupId>org.codehaus.mojo</groupId>

<artifactId>exec-maven-plugin</artifactId>

<version>1.1</version>

<executions>

<execution>

<goals>

<goal>java</goal>

</goals>

</execution>

</executions>

<configuration>

<mainClass>de.agdsn.jcroft.Main</mainClass>

</configuration>

</plugin>

<plugin>

<groupId>org.jacoco</groupId>

<artifactId>jacoco-maven-plugin</artifactId>

<version>0.7.6.201602180812</version>

<executions>

<execution>

<id>agent-for-ut</id>

<goals>

<goal>prepare-agent</goal>

</goals>

<configuration>

<append>true</append>

<destFile>${sonar.jacoco.reportPath}</destFile>

</configuration>

</execution>

<execution>

<id>agent-for-it</id>

<goals>

<goal>prepare-agent-integration</goal>

</goals>

<configuration>

<append>true</append>

<destFile>${sonar.jacoco.itReportPath}</destFile>

</configuration>

</execution>

</executions>

</plugin>

<plugin>

<groupId>org.codehaus.mojo</groupId>

<artifactId>cobertura-maven-plugin</artifactId>

<version>2.7</version>

<configuration>

<formats>

<format>html</format>

<format>xml</format>

</formats>

<check/>

</configuration>

</plugin>

</plugins>

</build>

</project>

JUnit Test:

package de.agdsn.jcroft.database.data;

import de.agdsn.jcroft.database.model.Property;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.Optional;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

@ExtendWith(SpringExtension.class)
@SpringBootTest
@ActiveProfiles("test")
class PropertyRepositoryTestJUnit5 {

  @Autowired
  protected PropertyRepository repository;

  @Test
  void saveProperty() {
    //check for problems with spring, if spring cannot find bean
    assertNotNull(repository);

    Property property = new Property("test", "title");
    repository.save(property);

    //find property
    Optional<Property> propertyOptional = repository.findById("test");
    assertEquals(true, propertyOptional.isPresent());

    Property property1 = propertyOptional.get();
    assertEquals("test", property1.getToken());
    assertEquals("title", property1.getTitle());

    //create another property with same token
    Property property2 = new Property("test", "title2");
    repository.save(property2);

    assertEquals(1, repository.findAll().size());

    Optional<Property> propertyOptional1 = repository.findById("test");
    assertEquals(true, propertyOptional1.isPresent());
    Property property3 = propertyOptional1.get();
    assertEquals("title2", property3.getTitle());

    //check equals
    assertEquals(false, property1.equals(property3));
    assertEquals(true, property2.equals(property3));
  }

}
@JuKu JuKu added this to the v0.0.1 First Early Access milestone Aug 4, 2018
@JuKu JuKu added enhancement New feature or request Priority: High type: test JUnit Tests, Integration tests and so on backlog labels Aug 4, 2018
@JuKu JuKu self-assigned this Aug 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backlog enhancement New feature or request Priority: High type: test JUnit Tests, Integration tests and so on
Projects
None yet
Development

No branches or pull requests

1 participant