- 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' }
- TestService.java
package org.blog.test.service; import org.blog.test.entity.TestTable; import java.util.List; public interface TestService { ListgetTestTableByName(String name); }
- TestServiceImpl.java
package org.blog.test.service.impl; import org.blog.test.entity.TestTable; import org.blog.test.repository.TestRepository; import org.blog.test.service.TestService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class TestServiceImpl implements TestService { @Autowired private TestRepository testRepository; @Override public ListgetTestTableByName(String name) { return testRepository.findByName(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); } }
- TestServiceTest.java
package org.blog.test.service; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; import java.util.ArrayList; import java.util.List; import org.blog.test.entity.TestTable; import org.blog.test.repository.TestRepository; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @WebMvcTest(TestService.class) public class TestServiceTest { @MockBean private TestRepository testRepository; @Autowired private TestService testService; @Test public void getByName() { //Given ListresultList = new ArrayList<>(); resultList.add(new TestTable(1L, "test")); given(testRepository.findByName("test")).willReturn(resultList); //When List result = testService.getTestTableByName("test"); //Then assertThat(result).isEqualTo(resultList); } }
- result
7:16:27: External tasks execution finished 'cleanTest test --tests org.blog.test.service.TestServiceTest'.
댓글 없음 :
댓글 쓰기