Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Entity
- @Getter
- @Setter
- @NoArgsConstructor
- @AllArgsConstructor
- public class Person {
- @Id
- private int id;
- private String name;
- }
- @Repository
- public interface PersonRepository extends JpaRepository<Person, Integer> {
- }
- @Service
- public class PersonService {
- @Autowired
- private PersonRepository repository;
- public Person createPerson(int id,String name) {
- return repository.save(new Person(id, name));
- }
- public List<Person> getPersons() {
- return repository.findAll();
- }
- }
- @RequestMapping
- @RestController
- public class PersonController {
- @Autowired
- private PersonService personService;
- @RequestMapping("/persons")
- public List<Person> getPersons() {
- return personService.getPersons();
- }
- @SpringBootApplication
- public class BootIntegrationTestApplication {
- public static void main(String[] args) {
- SpringApplication.run(BootIntegrationTestApplication.class, args);
- }
- }
- spring.datasource.url= jdbc:mysql://localhost:3306/test
- spring.datasource.username=root
- spring.datasource.password=password
- spring.jpa.hibernate.ddl-auto=create
- spring.jpa.show-sql=true
- @RunWith(SpringRunner.class)
- @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
- public class BootIntegrationTestApplicationTests {
- @Autowired
- private PersonService personService;
- @Autowired
- private TestRestTemplate restTemplate;
- @Test
- @Transactional
- public void contextLoads() {
- Person person = personService.createPerson(1, "person1");
- Assert.assertNotNull(person);
- ResponseEntity<Person[]> persons = restTemplate.getForEntity("/persons", Person[].class);
- }
- }
- @Test
- @Transactional
- public void testServiceSaveAndRead() {
- personService.createPerson(1, "person1");
- Assert.assertTrue(personService.getPersons().size() == 1);
- }
- @MockBean
- private PersonService personService;
- @Before
- public void setUp() {
- //mock the service
- given(personService.getPersons())
- .willReturn(Collections.singletonList(new Person(1, "p1")));
- }
- @Test
- public void testController() {
- ResponseEntity<Person[]> persons = restTemplate.getForEntity("/persons", Person[].class);
- Assert.assertTrue(persons.getBody()!=null && persons.getBody().length == 1);
- }
- @RunWith(SpringJUnit4ClassRunner.class)
- @SpringApplicationConfiguration(classes = Application.class)
- public class SmokeTest {
- @Rollback(false) //This is key to avoid rollback.
- @Test
- public void contexLoads() throws Exception {
- System.out.println("Hiren");
- System.out.println("started");
- _userDAO.save(new User("tyx", "x@x.com"));
- }
- @Autowired
- UserController userController;
- @Autowired
- UserDao _userDAO;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement