renamed back to testing-modules, pulled together testing-modules-2 modules into single module
This commit is contained in:
8
testing-modules/spring-testing/README.md
Normal file
8
testing-modules/spring-testing/README.md
Normal file
@@ -0,0 +1,8 @@
|
||||
## Relevant Articles:
|
||||
|
||||
- [Mockito.mock() vs @Mock vs @MockBean](http://www.baeldung.com/java-spring-mockito-mock-mockbean)
|
||||
- [A Quick Guide to @TestPropertySource](https://www.baeldung.com/spring-test-property-source)
|
||||
- [Guide to ReflectionTestUtils for Unit Testing](https://www.baeldung.com/spring-reflection-test-utils)
|
||||
- [How to Test the @Scheduled Annotation](https://www.baeldung.com/spring-testing-scheduled-annotation)
|
||||
- [Using SpringJUnit4ClassRunner with Parameterized](https://www.baeldung.com/springjunit4classrunner-parameterized)
|
||||
- [Override Properties in Spring’s Tests](https://www.baeldung.com/spring-tests-override-properties)
|
||||
101
testing-modules/spring-testing/pom.xml
Normal file
101
testing-modules/spring-testing/pom.xml
Normal file
@@ -0,0 +1,101 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.baeldung</groupId>
|
||||
<artifactId>spring-testing</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<name>spring-testing</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>java-hamcrest</artifactId>
|
||||
<version>${hamcrest.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<version>LATEST</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<version>LATEST</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>LATEST</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>LATEST</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.persistence</groupId>
|
||||
<artifactId>javax.persistence</artifactId>
|
||||
<version>${javax.persistence.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-jpa</artifactId>
|
||||
<version>LATEST</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit.jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.awaitility</groupId>
|
||||
<artifactId>awaitility</artifactId>
|
||||
<version>${awaitility.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${javax.servlet-api.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>spring-testing</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<hamcrest.version>2.0.0.0</hamcrest.version>
|
||||
<awaitility.version>3.1.6</awaitility.version>
|
||||
<junit.jupiter.version>5.4.0</junit.jupiter.version>
|
||||
<spring.version>5.1.4.RELEASE</spring.version>
|
||||
<javax.servlet-api.version>4.0.1</javax.servlet-api.version>
|
||||
<javax.persistence.version>2.1.1</javax.persistence.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.config;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@Configuration
|
||||
@EnableScheduling
|
||||
@ComponentScan("com.baeldung.scheduled")
|
||||
public class ScheduledConfig {
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
@EnableWebMvc
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = {"com.baeldung.controller.parameterized"})
|
||||
public class WebConfig {
|
||||
|
||||
@Autowired
|
||||
private ServletContext ctx;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.controller.parameterized;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Controller
|
||||
public class EmployeeRoleController {
|
||||
|
||||
private static Map<String, Role> userRoleCache = new HashMap<>();
|
||||
|
||||
static {
|
||||
userRoleCache.put("John", Role.ADMIN);
|
||||
userRoleCache.put("Doe", Role.EMPLOYEE);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/role/{name}", method = RequestMethod.GET, produces = "application/text")
|
||||
@ResponseBody
|
||||
public String getEmployeeRole(@PathVariable("name") String employeeName) {
|
||||
|
||||
return userRoleCache.get(employeeName).toString();
|
||||
}
|
||||
|
||||
private enum Role {
|
||||
ADMIN, EMPLOYEE
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.overrideproperties;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.overrideproperties.resolver;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class PropertySourceResolver {
|
||||
|
||||
private final String firstProperty;
|
||||
private final String secondProperty;
|
||||
|
||||
public PropertySourceResolver(@Value("${example.firstProperty}") String firstProperty, @Value("${example.secondProperty}") String secondProperty) {
|
||||
this.firstProperty = firstProperty;
|
||||
this.secondProperty = secondProperty;
|
||||
}
|
||||
|
||||
public String getFirstProperty() {
|
||||
return firstProperty;
|
||||
}
|
||||
|
||||
public String getSecondProperty() {
|
||||
return secondProperty;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.scheduled;
|
||||
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@Component
|
||||
public class Counter {
|
||||
private final AtomicInteger count = new AtomicInteger(0);
|
||||
|
||||
@Scheduled(fixedDelay = 5)
|
||||
public void scheduled() {
|
||||
this.count.incrementAndGet();
|
||||
}
|
||||
|
||||
public int getInvocationCount() {
|
||||
return this.count.get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.testpropertysource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ClassUsingProperty {
|
||||
|
||||
@Value("${baeldung.testpropertysource.one}")
|
||||
private String propertyOne;
|
||||
|
||||
public String retrievePropertyOne() {
|
||||
return propertyOne;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package org.baeldung.mockito.repository;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Integer id;
|
||||
private String name;
|
||||
private Integer status;
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public User(String name, Integer status) {
|
||||
this.name = name;
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Integer status) {
|
||||
this.status = status;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.baeldung.mockito.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository("userRepository")
|
||||
public interface UserRepository extends JpaRepository<User, Integer> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.baeldung.reflectiontestutils.repository;
|
||||
|
||||
public class Employee {
|
||||
private Integer id;
|
||||
private String name;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
private String employeeToString() {
|
||||
return "id: " + getId() + "; name: " + getName();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package org.baeldung.reflectiontestutils.repository;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class EmployeeService {
|
||||
@Autowired
|
||||
private HRService hrService;
|
||||
|
||||
public String findEmployeeStatus(Integer employeeId) {
|
||||
return "Employee " + employeeId + " status: " + hrService.getEmployeeStatus(employeeId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.baeldung.reflectiontestutils.repository;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class HRService {
|
||||
|
||||
public String getEmployeeStatus(Integer employeeId) {
|
||||
return "Inactive";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -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()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
# override properties
|
||||
example.firstProperty=profile
|
||||
@@ -0,0 +1,3 @@
|
||||
# override properties
|
||||
example.firstProperty=file
|
||||
example.secondProperty=file
|
||||
@@ -0,0 +1 @@
|
||||
baeldung.testpropertysource.one=default-value
|
||||
@@ -0,0 +1 @@
|
||||
example.secondProperty=contextFile
|
||||
@@ -0,0 +1 @@
|
||||
baeldung.testpropertysource.one=other-location-value
|
||||
Reference in New Issue
Block a user