renamed back to testing-modules, pulled together testing-modules-2 modules into single module
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.controller.parameterized;
|
||||
|
||||
import com.baeldung.config.WebConfig;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = WebConfig.class)
|
||||
public class RoleControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
private static final String CONTENT_TYPE = "application/text;charset=ISO-8859-1";
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeNameJohnWhenInvokeRoleThenReturnAdmin() throws Exception {
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.get("/role/John")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE))
|
||||
.andExpect(MockMvcResultMatchers.content().string("ADMIN"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeNameDoeWhenInvokeRoleThenReturnEmployee() throws Exception {
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.get("/role/Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE))
|
||||
.andExpect(MockMvcResultMatchers.content().string("EMPLOYEE"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.baeldung.controller.parameterized;
|
||||
|
||||
import com.baeldung.config.WebConfig;
|
||||
import org.junit.Before;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameter;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestContextManager;
|
||||
import org.springframework.test.context.junit4.rules.SpringClassRule;
|
||||
import org.springframework.test.context.junit4.rules.SpringMethodRule;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = WebConfig.class)
|
||||
public class RoleControllerParameterizedClassRuleIntegrationTest {
|
||||
|
||||
private static final String CONTENT_TYPE = "application/text;charset=ISO-8859-1";
|
||||
|
||||
@ClassRule
|
||||
public static final SpringClassRule scr = new SpringClassRule();
|
||||
|
||||
@Rule
|
||||
public final SpringMethodRule smr = new SpringMethodRule();
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Parameter(value = 0)
|
||||
public String name;
|
||||
|
||||
@Parameter(value = 1)
|
||||
public String role;
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> data() {
|
||||
Collection<Object[]> params = new ArrayList();
|
||||
params.add(new Object[]{"John", "ADMIN"});
|
||||
params.add(new Object[]{"Doe", "EMPLOYEE"});
|
||||
|
||||
return params;
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeNameWhenInvokeRoleThenReturnRole() throws Exception {
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.get("/role/" + name)).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE))
|
||||
.andExpect(MockMvcResultMatchers.content().string(role));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.baeldung.controller.parameterized;
|
||||
|
||||
import com.baeldung.config.WebConfig;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameter;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestContextManager;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = WebConfig.class)
|
||||
public class RoleControllerParameterizedIntegrationTest {
|
||||
|
||||
private static final String CONTENT_TYPE = "application/text;charset=ISO-8859-1";
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
private TestContextManager testContextManager;
|
||||
|
||||
@Parameter(value = 0)
|
||||
public String name;
|
||||
|
||||
@Parameter(value = 1)
|
||||
public String role;
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> data() {
|
||||
Collection<Object[]> params = new ArrayList();
|
||||
params.add(new Object[]{"John", "ADMIN"});
|
||||
params.add(new Object[]{"Doe", "EMPLOYEE"});
|
||||
|
||||
return params;
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.testContextManager = new TestContextManager(getClass());
|
||||
this.testContextManager.prepareTestInstance(this);
|
||||
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmployeeNameWhenInvokeRoleThenReturnRole() throws Exception {
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.get("/role/" + name)).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE))
|
||||
.andExpect(MockMvcResultMatchers.content().string(role));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.overrideproperties;
|
||||
|
||||
import com.baeldung.overrideproperties.resolver.PropertySourceResolver;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(initializers = PropertyOverrideContextInitializer.class, classes = Application.class)
|
||||
public class ContextPropertySourceResolverIntegrationTest {
|
||||
|
||||
@Autowired private PropertySourceResolver propertySourceResolver;
|
||||
|
||||
@Test
|
||||
public void shouldContext_overridePropertyValues() {
|
||||
final String firstProperty = propertySourceResolver.getFirstProperty();
|
||||
final String secondProperty = propertySourceResolver.getSecondProperty();
|
||||
|
||||
assertEquals(PropertyOverrideContextInitializer.PROPERTY_FIRST_VALUE, firstProperty);
|
||||
assertEquals("contextFile", secondProperty);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.overrideproperties;
|
||||
|
||||
import com.baeldung.overrideproperties.resolver.PropertySourceResolver;
|
||||
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.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
public class ProfilePropertySourceResolverIntegrationTest {
|
||||
|
||||
@Autowired private PropertySourceResolver propertySourceResolver;
|
||||
|
||||
@Test
|
||||
public void shouldProfiledProperty_overridePropertyValues() {
|
||||
final String firstProperty = propertySourceResolver.getFirstProperty();
|
||||
final String secondProperty = propertySourceResolver.getSecondProperty();
|
||||
|
||||
assertEquals("profile", firstProperty);
|
||||
assertEquals("file", secondProperty);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.overrideproperties;
|
||||
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.test.context.support.TestPropertySourceUtils;
|
||||
|
||||
public class PropertyOverrideContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||
|
||||
static final String PROPERTY_FIRST_VALUE = "contextClass";
|
||||
|
||||
@Override
|
||||
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
|
||||
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(configurableApplicationContext, "example.firstProperty=" + PROPERTY_FIRST_VALUE);
|
||||
|
||||
TestPropertySourceUtils.addPropertiesFilesToEnvironment(configurableApplicationContext, "context-override-application.properties");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.overrideproperties;
|
||||
|
||||
import com.baeldung.overrideproperties.resolver.PropertySourceResolver;
|
||||
import org.junit.Assert;
|
||||
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.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(properties = { "example.firstProperty=annotation" })
|
||||
public class SpringBootPropertySourceResolverIntegrationTest {
|
||||
|
||||
@Autowired private PropertySourceResolver propertySourceResolver;
|
||||
|
||||
@Test
|
||||
public void shouldSpringBootTestAnnotation_overridePropertyValues() {
|
||||
final String firstProperty = propertySourceResolver.getFirstProperty();
|
||||
final String secondProperty = propertySourceResolver.getSecondProperty();
|
||||
|
||||
Assert.assertEquals("annotation", firstProperty);
|
||||
Assert.assertEquals("file", secondProperty);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.overrideproperties;
|
||||
|
||||
import com.baeldung.overrideproperties.resolver.PropertySourceResolver;
|
||||
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.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class TestResourcePropertySourceResolverIntegrationTest {
|
||||
|
||||
@Autowired private PropertySourceResolver propertySourceResolver;
|
||||
|
||||
@Test
|
||||
public void shouldTestResourceFile_overridePropertyValues() {
|
||||
final String firstProperty = propertySourceResolver.getFirstProperty();
|
||||
final String secondProperty = propertySourceResolver.getSecondProperty();
|
||||
|
||||
assertEquals("file", firstProperty);
|
||||
assertEquals("file", secondProperty);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.scheduled;
|
||||
|
||||
import com.baeldung.config.ScheduledConfig;
|
||||
import org.awaitility.Duration;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.mock.mockito.SpyBean;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.mockito.Mockito.atLeast;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@SpringJUnitConfig(ScheduledConfig.class)
|
||||
public class ScheduledAwaitilityIntegrationTest {
|
||||
|
||||
@SpyBean private Counter counter;
|
||||
|
||||
@Test
|
||||
public void whenWaitOneSecond_thenScheduledIsCalledAtLeastTenTimes() {
|
||||
await()
|
||||
.atMost(Duration.ONE_SECOND)
|
||||
.untilAsserted(() -> verify(counter, atLeast(10)).scheduled());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.scheduled;
|
||||
|
||||
import com.baeldung.config.ScheduledConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@SpringJUnitConfig(ScheduledConfig.class)
|
||||
public class ScheduledIntegrationTest {
|
||||
|
||||
@Autowired Counter counter;
|
||||
|
||||
@Test
|
||||
public void givenSleepBy100ms_whenGetInvocationCount_thenIsGreaterThanZero() throws InterruptedException {
|
||||
Thread.sleep(100L);
|
||||
|
||||
assertThat(counter.getInvocationCount()).isGreaterThan(0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.testpropertysource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = ClassUsingProperty.class)
|
||||
@TestPropertySource
|
||||
public class DefaultTestPropertySourceIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
ClassUsingProperty classUsingProperty;
|
||||
|
||||
@Test
|
||||
public void givenDefaultTestPropertySource_whenVariableOneRetrieved_thenValueInDefaultFileReturned() {
|
||||
String output = classUsingProperty.retrievePropertyOne();
|
||||
|
||||
assertThat(output).isEqualTo("default-value");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.testpropertysource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = ClassUsingProperty.class)
|
||||
@TestPropertySource(locations = "/other-location.properties")
|
||||
public class LocationTestPropertySourceIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
ClassUsingProperty classUsingProperty;
|
||||
|
||||
@Test
|
||||
public void givenDefaultTestPropertySource_whenVariableOneRetrieved_thenValueInDefaultFileReturned() {
|
||||
String output = classUsingProperty.retrievePropertyOne();
|
||||
|
||||
assertThat(output).isEqualTo("other-location-value");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.testpropertysource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = ClassUsingProperty.class)
|
||||
@TestPropertySource(locations = "/other-location.properties", properties = "baeldung.testpropertysource.one=other-properties-value")
|
||||
public class PropertiesTestPropertySourceIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
ClassUsingProperty classUsingProperty;
|
||||
|
||||
@Test
|
||||
public void givenDefaultTestPropertySource_whenVariableOneRetrieved_thenValueInDefaultFileReturned() {
|
||||
String output = classUsingProperty.retrievePropertyOne();
|
||||
|
||||
assertThat(output).isEqualTo("other-properties-value");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.baeldung.mockito;
|
||||
|
||||
import org.baeldung.mockito.repository.UserRepository;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class MockAnnotationUnitTest {
|
||||
|
||||
@Mock
|
||||
UserRepository mockRepository;
|
||||
|
||||
@Test
|
||||
public void givenCountMethodMocked_WhenCountInvoked_ThenMockValueReturned() {
|
||||
Mockito.when(mockRepository.count()).thenReturn(123L);
|
||||
|
||||
long userCount = mockRepository.count();
|
||||
|
||||
Assert.assertEquals(123L, userCount);
|
||||
Mockito.verify(mockRepository).count();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCountMethodOfLocalMockVariableMocked_WhenCountInvoked_ThenMockedValueReturned() {
|
||||
UserRepository localMockRepository = Mockito.mock(UserRepository.class);
|
||||
Mockito.when(localMockRepository.count()).thenReturn(111L);
|
||||
|
||||
long userCount = localMockRepository.count();
|
||||
|
||||
Assert.assertEquals(111L, userCount);
|
||||
Mockito.verify(localMockRepository).count();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.baeldung.mockito;
|
||||
|
||||
import org.baeldung.mockito.repository.UserRepository;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
public class MockBeanAnnotationIntegrationTest {
|
||||
|
||||
@MockBean
|
||||
UserRepository mockRepository;
|
||||
|
||||
@Autowired
|
||||
ApplicationContext context;
|
||||
|
||||
@Test
|
||||
public void givenCountMethodMocked_WhenCountInvoked_ThenMockValueReturned() {
|
||||
Mockito.when(mockRepository.count()).thenReturn(123L);
|
||||
|
||||
UserRepository userRepoFromContext = context.getBean(UserRepository.class);
|
||||
long userCount = userRepoFromContext.count();
|
||||
|
||||
Assert.assertEquals(123L, userCount);
|
||||
Mockito.verify(mockRepository).count();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package org.baeldung.reflectiontestutils;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import org.baeldung.reflectiontestutils.repository.Employee;
|
||||
import org.baeldung.reflectiontestutils.repository.EmployeeService;
|
||||
import org.baeldung.reflectiontestutils.repository.HRService;
|
||||
import org.junit.Test;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class ReflectionTestUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenNonPublicField_thenReflectionTestUtilsSetField() {
|
||||
Employee employee = new Employee();
|
||||
ReflectionTestUtils.setField(employee, "id", 1);
|
||||
assertTrue(employee.getId().equals(1));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNonPublicMethod_thenReflectionTestUtilsInvokeMethod() {
|
||||
Employee employee = new Employee();
|
||||
ReflectionTestUtils.setField(employee, "id", 1);
|
||||
employee.setName("Smith, John");
|
||||
assertTrue(ReflectionTestUtils.invokeMethod(employee, "employeeToString").equals("id: 1; name: Smith, John"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInjectingMockOfDependency_thenReflectionTestUtilsSetField() {
|
||||
Employee employee = new Employee();
|
||||
ReflectionTestUtils.setField(employee, "id", 1);
|
||||
employee.setName("Smith, John");
|
||||
|
||||
HRService hrService = mock(HRService.class);
|
||||
when(hrService.getEmployeeStatus(employee.getId())).thenReturn("Active");
|
||||
EmployeeService employeeService = new EmployeeService();
|
||||
|
||||
// Inject mock into the private field
|
||||
ReflectionTestUtils.setField(employeeService, "hrService", hrService);
|
||||
assertEquals("Employee " + employee.getId() + " status: Active", employeeService.findEmployeeStatus(employee.getId()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user