Skip to content
Permalink
Browse files

How to configure TestEntityManager

  • Loading branch information...
slem1 committed Oct 26, 2017
1 parent b54ad14 commit f812d07a92782c932c3a3d2865debbfbcebebe59
@@ -3,6 +3,7 @@
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@@ -11,7 +12,7 @@
public class MyEntity {

@Id
@GeneratedValue
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

@@ -0,0 +1,11 @@
package fr.sle.testhowto;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

/**
* @author slemoine
*/
@Repository
public interface MyEntityJpaRepository extends JpaRepository<MyEntity, Long> {
}
@@ -0,0 +1,21 @@
package fr.sle.testhowto;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author slemoine
*/
@Service
@Transactional
public class MyEntityService {

@Autowired
private MyEntityJpaRepository myEntityJpaRepository;

public void createnewMyEntity(){
myEntityJpaRepository.save(new MyEntity());
}

}
@@ -24,6 +24,6 @@

@Test
public void myComponentTestMethod() {
component.print();
}

}
@@ -0,0 +1,30 @@
package fr.sle.testhowto.test;

import fr.sle.testhowto.MyEntity;
import fr.sle.testhowto.test.config.AdditionalTestConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureTestEntityManager;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

/**
* @author slemoine
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AdditionalTestConfig.class)
@AutoConfigureTestEntityManager
@Transactional
public class MyEntityServiceTest {

@Autowired
private TestEntityManager testEntityManager;

@Test
public void saveMyEntityShouldCreateNewEntity() {
testEntityManager.persistAndGetId(new MyEntity());
}
}
@@ -1,4 +1,6 @@
spring.profiles.active=@spring.profiles.active@
spring.datasource.username=testhowto
spring.datasource.password=testhowto
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.driver-class-name=org.postgresql.Driver

spring.jpa.generate-ddl=true

0 comments on commit f812d07

Please sign in to comment.
You can’t perform that action at this time.