Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. @Entity
  2. @Getter
  3. @Setter
  4. @NoArgsConstructor
  5. @AllArgsConstructor
  6. public class Person {
  7. @Id
  8. private int id;
  9. private String name;
  10. }
  11.  
  12. @Repository
  13. public interface PersonRepository extends JpaRepository<Person, Integer> {
  14. }
  15.  
  16. @Service
  17. public class PersonService {
  18.  
  19. @Autowired
  20. private PersonRepository repository;
  21.  
  22. public Person createPerson(int id,String name) {
  23. return repository.save(new Person(id, name));
  24. }
  25.  
  26. public List<Person> getPersons() {
  27. return repository.findAll();
  28. }
  29. }
  30.  
  31. @RequestMapping
  32. @RestController
  33. public class PersonController {
  34.  
  35. @Autowired
  36. private PersonService personService;
  37.  
  38. @RequestMapping("/persons")
  39. public List<Person> getPersons() {
  40. return personService.getPersons();
  41. }
  42.  
  43. @SpringBootApplication
  44. public class BootIntegrationTestApplication {
  45.  
  46. public static void main(String[] args) {
  47. SpringApplication.run(BootIntegrationTestApplication.class, args);
  48. }
  49. }
  50.  
  51. spring.datasource.url= jdbc:mysql://localhost:3306/test
  52. spring.datasource.username=root
  53. spring.datasource.password=password
  54. spring.jpa.hibernate.ddl-auto=create
  55. spring.jpa.show-sql=true
  56.  
  57. @RunWith(SpringRunner.class)
  58. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  59. public class BootIntegrationTestApplicationTests {
  60.  
  61. @Autowired
  62. private PersonService personService;
  63. @Autowired
  64. private TestRestTemplate restTemplate;
  65.  
  66. @Test
  67. @Transactional
  68. public void contextLoads() {
  69. Person person = personService.createPerson(1, "person1");
  70. Assert.assertNotNull(person);
  71.  
  72. ResponseEntity<Person[]> persons = restTemplate.getForEntity("/persons", Person[].class);
  73. }
  74. }
  75.  
  76. @Test
  77. @Transactional
  78. public void testServiceSaveAndRead() {
  79. personService.createPerson(1, "person1");
  80. Assert.assertTrue(personService.getPersons().size() == 1);
  81. }
  82.  
  83. @MockBean
  84. private PersonService personService;
  85.  
  86. @Before
  87. public void setUp() {
  88. //mock the service
  89. given(personService.getPersons())
  90. .willReturn(Collections.singletonList(new Person(1, "p1")));
  91. }
  92.  
  93. @Test
  94. public void testController() {
  95. ResponseEntity<Person[]> persons = restTemplate.getForEntity("/persons", Person[].class);
  96. Assert.assertTrue(persons.getBody()!=null && persons.getBody().length == 1);
  97. }
  98.  
  99. @RunWith(SpringJUnit4ClassRunner.class)
  100. @SpringApplicationConfiguration(classes = Application.class)
  101.  
  102. public class SmokeTest {
  103.  
  104. @Rollback(false) //This is key to avoid rollback.
  105. @Test
  106. public void contexLoads() throws Exception {
  107. System.out.println("Hiren");
  108.  
  109. System.out.println("started");
  110. _userDAO.save(new User("tyx", "x@x.com"));
  111. }
  112.  
  113. @Autowired
  114. UserController userController;
  115.  
  116. @Autowired
  117. UserDao _userDAO;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement