- structure
- build.gradle
group 'org.blog.test'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'spring-boot'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
}
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '1.4.0.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.4.0.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '1.4.0.RELEASE'
compile group: 'com.h2database', name: 'h2', version: '1.4.192'
compile group: 'org.projectlombok', name: 'lombok', version: '1.16.10'
compile group: 'junit', name: 'junit', version: '4.12'
}
- TestTable.java
package org.blog.test.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TestTable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
public TestTable(String name) {
this.name = name;
}
}
- TestRepository.java
package org.blog.test.repository; import org.blog.test.entity.TestTable; import org.springframework.data.repository.CrudRepository; import java.util.List; public interface TestRepository extends CrudRepository{ List findByName(String name); }
- JunitApplication.java
package org.blog.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class JunitApplication {
public static void main(String[] args) {
SpringApplication.run(JunitApplication.class, args);
}
}
- TestRepositoryTest.java
package org.blog.test.repository;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import java.util.List;
import org.blog.test.entity.TestTable;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@DataJpaTest
public class TestRepositoryTest {
@Autowired
private TestEntityManager testEntityManager;
@Autowired
private TestRepository testRepository;
@Test
public void findByName() {
//Given
TestTable testTable = new TestTable("test");
this.testEntityManager.persist(testTable);
//When
List result = testRepository.findByName("test");
//Then
assertThat(result.get(0), is(testTable));
}
}
- result
7:06:47: External tasks execution finished 'cleanTest test --tests org.blog.test.repository.TestRepositoryTest'.

댓글 없음 :
댓글 쓰기