Skip to content
Permalink
Browse files

How to inject sql dataset

  • Loading branch information...
slem1 committed Oct 26, 2017
1 parent f812d07 commit 2860d42fa59240f303e7b81219b00c694c9679f1
@@ -4,6 +4,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author slemoine
*/
@@ -14,8 +16,11 @@
@Autowired
private MyEntityJpaRepository myEntityJpaRepository;

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

public List<MyEntity> getAllMyEntity() {
return myEntityJpaRepository.findAll();
}
}
@@ -1,16 +1,21 @@
package fr.sle.testhowto.test;

import fr.sle.testhowto.MyEntity;
import fr.sle.testhowto.MyEntityService;
import fr.sle.testhowto.test.config.AdditionalTestConfig;
import org.junit.Assert;
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.jdbc.Sql;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author slemoine
*/
@@ -23,8 +28,18 @@
@Autowired
private TestEntityManager testEntityManager;

@Autowired
private MyEntityService myEntityService;

@Test
public void saveMyEntityShouldCreateNewEntity() {
testEntityManager.persistAndGetId(new MyEntity());
}

@Test
@Sql("/datasets/MyEntityServiceTest/injectedSqlTestMethod.sql")
public void injectedSqlTestMethod() {
List<MyEntity> allMyEntity = myEntityService.getAllMyEntity();
Assert.assertEquals(1, allMyEntity.size());
}
}
@@ -3,4 +3,4 @@ spring.datasource.username=testhowto
spring.datasource.password=testhowto
spring.datasource.driver-class-name=org.postgresql.Driver

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create-drop
@@ -0,0 +1 @@
insert into my_entity values (1, 'my_value');

0 comments on commit 2860d42

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