BAEL-1907 Created new module spring-testing
This commit is contained in:
2
testing-modules/spring-testing/README.md
Normal file
2
testing-modules/spring-testing/README.md
Normal file
@@ -0,0 +1,2 @@
|
||||
### Relevant Articles:
|
||||
|
||||
75
testing-modules/spring-testing/pom.xml
Normal file
75
testing-modules/spring-testing/pom.xml
Normal file
@@ -0,0 +1,75 @@
|
||||
<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>
|
||||
<scope>test</scope>
|
||||
</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.eclipse.persistence</groupId>
|
||||
<artifactId>javax.persistence</artifactId>
|
||||
<version>2.1.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-jpa</artifactId>
|
||||
<version>LATEST</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>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -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,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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user