JAVA-10516: Revisit spring-boot module inside spring-boot-modules folder (#12038)
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.typeconversion.converter;
|
||||
|
||||
import com.baeldung.typeconversion.Application;
|
||||
import com.baeldung.typeconversion.domain.Modes;
|
||||
import com.baeldung.typeconversion.entity.Employee;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = Application.class)
|
||||
@WebAppConfiguration
|
||||
public class CustomConverterIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
ConversionService conversionService;
|
||||
|
||||
@Test
|
||||
public void whenConvertStringToIntegerUsingDefaultConverter_thenSuccess() {
|
||||
assertThat(conversionService.convert("25", Integer.class)).isEqualTo(25);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertStringToEmployee_thenSuccess() {
|
||||
Employee employee = conversionService.convert("1,50000.00", Employee.class);
|
||||
Employee actualEmployee = new Employee(1, 50000.00);
|
||||
assertThat(conversionService.convert("1,50000.00", Employee.class)).isEqualToComparingFieldByField(actualEmployee);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConvertStringToEnum_thenSuccess() {
|
||||
assertThat(conversionService.convert("ALPHA", Modes.class)).isEqualTo(Modes.ALPHA);
|
||||
}
|
||||
|
||||
@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(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));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.typeconversion.converter.controller;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import com.baeldung.typeconversion.Application;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK, classes = Application.class)
|
||||
@AutoConfigureMockMvc
|
||||
public class StringToEmployeeConverterControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
public void getStringToEmployeeTest() throws Exception {
|
||||
mockMvc.perform(get("/string-to-employee?employee=1,2000")).andDo(print()).andExpect(jsonPath("$.id", is(1))).andExpect(jsonPath("$.salary", is(2000.0))).andExpect(status().isOk());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user