Merge pull request #4845 from amit2103/BAEL-7609

[BAEL-7609] - Fixed spring-boot integration tests
This commit is contained in:
Loredana Crusoveanu 2018-07-29 17:47:50 +03:00 committed by GitHub
commit 51d79fdc55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 66 additions and 20 deletions

View File

@ -3,6 +3,7 @@ package com.baeldung.displayallbeans.controller;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@ -14,9 +15,9 @@ public class FooController {
private FooService fooService;
@GetMapping(value = "/displayallbeans")
public String getHeaderAndBody(Map<String, Object> model) {
public ResponseEntity<String> getHeaderAndBody(Map<String, Object> model) {
model.put("header", fooService.getHeader());
model.put("message", fooService.getBody());
return "displayallbeans";
return ResponseEntity.ok("displayallbeans");
}
}

View File

@ -0,0 +1,15 @@
package org.baeldung.boot.config;
import org.baeldung.boot.converter.StringToEmployeeConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new StringToEmployeeConverter());
}
}

View File

@ -0,0 +1,16 @@
package org.baeldung.boot.converter;
import org.springframework.core.convert.converter.Converter;
import com.baeldung.toggle.Employee;
public class StringToEmployeeConverter implements Converter<String, Employee> {
@Override
public Employee convert(String from) {
String[] data = from.split(",");
return new Employee(
Long.parseLong(data[0]),
Double.parseDouble(data[1]));
}
}

View File

@ -0,0 +1,17 @@
package org.baeldung.boot.converter.controller;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.toggle.Employee;
@RestController
public class StringToEmployeeConverterController {
@GetMapping("/string-to-employee")
public ResponseEntity<Object> getStringToEmployee(@RequestParam("employee") Employee employee) {
return ResponseEntity.ok(employee);
}
}

View File

@ -4,17 +4,15 @@ import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import org.baeldung.boot.ApplicationIntegrationTest;
import org.baeldung.boot.DemoApplicationIntegrationTest;
import org.baeldung.demo.model.Foo;
import org.baeldung.session.exception.repository.FooRepository;
import org.baeldung.demo.repository.FooRepository;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.TestPropertySource;
import org.springframework.transaction.annotation.Transactional;
@Transactional
@TestPropertySource("classpath:exception-hibernate.properties")
public class HibernateSessionIntegrationTest extends ApplicationIntegrationTest {
public class HibernateSessionIntegrationTest extends DemoApplicationIntegrationTest {
@Autowired
private FooRepository fooRepository;
@ -23,7 +21,7 @@ public class HibernateSessionIntegrationTest extends ApplicationIntegrationTest
Foo foo = new Foo("Exception Solved");
fooRepository.save(foo);
foo = null;
foo = fooRepository.get(1);
foo = fooRepository.findByName("Exception Solved");
assertThat(foo, notNullValue());
assertThat(foo.getId(), is(1));

View File

@ -6,9 +6,11 @@ import org.baeldung.session.exception.repository.FooRepository;
import org.hibernate.HibernateException;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.TestPropertySource;
import org.springframework.transaction.annotation.Transactional;
@Transactional
@TestPropertySource("classpath:exception-hibernate.properties")
public class NoHibernateSessionIntegrationTest extends ApplicationIntegrationTest {
@Autowired
private FooRepository fooRepository;

View File

@ -43,7 +43,7 @@ public class CustomConverterIntegrationTest {
@Test
public void whenConvertingToBigDecimalUsingGenericConverter_thenSuccess() {
assertThat(conversionService.convert(Integer.valueOf(11), BigDecimal.class)).isEqualTo(BigDecimal.valueOf(11.00).setScale(2, BigDecimal.ROUND_HALF_EVEN));
assertThat(conversionService.convert(Integer.valueOf(11), BigDecimal.class)).isEqualTo(BigDecimal.valueOf(11.00).setScale(0, BigDecimal.ROUND_HALF_EVEN));
assertThat(conversionService.convert(Double.valueOf(25.23), BigDecimal.class)).isEqualByComparingTo(BigDecimal.valueOf(Double.valueOf(25.23)));
assertThat(conversionService.convert("2.32", BigDecimal.class)).isEqualTo(BigDecimal.valueOf(2.32));
}

View File

@ -1,9 +1,11 @@
package org.baeldung.demo.boottest;
import org.baeldung.demo.boottest.Employee;
import org.baeldung.demo.boottest.EmployeeRepository;
import org.baeldung.demo.boottest.EmployeeService;
import org.baeldung.demo.boottest.EmployeeServiceImpl;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -15,11 +17,6 @@ import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Arrays;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
public class EmployeeServiceImplIntegrationTest {
@ -50,9 +47,9 @@ public class EmployeeServiceImplIntegrationTest {
Mockito.when(employeeRepository.findByName(john.getName())).thenReturn(john);
Mockito.when(employeeRepository.findByName(alex.getName())).thenReturn(alex);
Mockito.when(employeeRepository.findByName("wrong_name")).thenReturn(null);
Mockito.when(employeeRepository.findById(john.getId()).orElse(null)).thenReturn(john);
Mockito.when(employeeRepository.findById(john.getId())).thenReturn(Optional.of(john));
Mockito.when(employeeRepository.findAll()).thenReturn(allEmployees);
Mockito.when(employeeRepository.findById(-99L).orElse(null)).thenReturn(null);
Mockito.when(employeeRepository.findById(-99L)).thenReturn(Optional.empty());
}
@Test