Skip to content

Commit b406b3e

Browse files
committed
Initial Commit
0 parents  commit b406b3e

13 files changed

+584
-0
lines changed

pom.xml

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.example</groupId>
7+
<artifactId>springbootTutorial</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>springbootJUnit5_Tutorial</name>
12+
<description>Demo project for Spring Boot JUnit 5</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.2.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-web</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-test</artifactId>
34+
<scope>test</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.junit.jupiter</groupId>
38+
<artifactId>junit-jupiter-api</artifactId>
39+
<version>5.0.0-M4</version>
40+
<type>jar</type>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.mockito</groupId>
44+
<artifactId>mockito-core</artifactId>
45+
<version>2.7.17</version>
46+
<scope>test</scope>
47+
<type>jar</type>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.junit.jupiter</groupId>
51+
<artifactId>junit-jupiter-engine</artifactId>
52+
<version>5.0.0-M4</version>
53+
<scope>test</scope>
54+
</dependency>
55+
56+
57+
<dependency>
58+
<groupId>org.junit.platform</groupId>
59+
<artifactId>junit-platform-runner</artifactId>
60+
<version>1.0.0-M4</version>
61+
<scope>test</scope>
62+
<exclusions>
63+
<exclusion>
64+
<groupId>junit</groupId>
65+
<artifactId>junit</artifactId>
66+
</exclusion>
67+
</exclusions>
68+
</dependency>
69+
70+
<dependency>
71+
<groupId>com.github.sbrannen</groupId>
72+
<artifactId>spring-test-junit5</artifactId>
73+
<version>1.0.0.M4</version>
74+
<scope>test</scope>
75+
<type>jar</type>
76+
</dependency>
77+
</dependencies>
78+
79+
<build>
80+
<plugins>
81+
<plugin>
82+
<groupId>org.springframework.boot</groupId>
83+
<artifactId>spring-boot-maven-plugin</artifactId>
84+
</plugin>
85+
86+
<plugin>
87+
<groupId>org.apache.maven.plugins</groupId>
88+
<artifactId>maven-failsafe-plugin</artifactId>
89+
<executions>
90+
<execution>
91+
<goals>
92+
<goal>integration-test</goal>
93+
<goal>verify</goal>
94+
</goals>
95+
</execution>
96+
</executions>
97+
</plugin>
98+
</plugins>
99+
</build>
100+
101+
<repositories>
102+
<repository>
103+
<id>jitpack.io</id>
104+
<url>https://jitpack.io</url>
105+
</repository>
106+
</repositories>
107+
108+
109+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.mvpjava;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class Company {
7+
String companyName;
8+
Set<Employee> employees = new HashSet<>();
9+
10+
11+
public Company(String companyName) {
12+
this.companyName = companyName;
13+
}
14+
15+
public String getCompanyName() {
16+
return companyName;
17+
}
18+
19+
public Set<Employee> getEmployees() {
20+
return employees;
21+
}
22+
23+
public boolean addEmployee(Employee newHire){
24+
return employees.add(newHire);
25+
}
26+
27+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.mvpjava;
2+
3+
import java.math.BigDecimal;
4+
import java.util.Objects;
5+
6+
public class Employee {
7+
8+
private String name;
9+
Role role;
10+
float salary; //yes, I know I should use BigDecimal!
11+
12+
public Employee(String name) {
13+
this.name = name;
14+
}
15+
16+
public Employee(String name, Role role) {
17+
this(name);
18+
this.role = role;
19+
}
20+
21+
public Employee() {
22+
this("not-set");
23+
}
24+
25+
/**
26+
* Get the value of name
27+
*
28+
* @return the value of name
29+
*/
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public Role getRole() {
35+
return role;
36+
}
37+
38+
public void setRole(Role role) {
39+
this.role = role;
40+
}
41+
42+
public float getSalary() {
43+
return salary;
44+
}
45+
46+
public void setSalary(float salary) {
47+
this.salary = salary;
48+
}
49+
50+
@Override
51+
public int hashCode() {
52+
int hash = 7;
53+
hash = 29 * hash + Objects.hashCode(this.name);
54+
hash = 29 * hash + Objects.hashCode(this.role);
55+
hash = 29 * hash + Float.floatToIntBits(this.salary);
56+
return hash;
57+
}
58+
59+
@Override
60+
public boolean equals(Object obj) {
61+
if (this == obj) {
62+
return true;
63+
}
64+
if (obj == null) {
65+
return false;
66+
}
67+
if (getClass() != obj.getClass()) {
68+
return false;
69+
}
70+
final Employee other = (Employee) obj;
71+
if (Float.floatToIntBits(this.salary) != Float.floatToIntBits(other.salary)) {
72+
return false;
73+
}
74+
if (!Objects.equals(this.name, other.name)) {
75+
return false;
76+
}
77+
if (!Objects.equals(this.role, other.role)) {
78+
return false;
79+
}
80+
return true;
81+
}
82+
83+
@Override
84+
public String toString() {
85+
return "Employee{" + "name=" + name + ", role=" + role + ", salary=" + salary + '}';
86+
}
87+
88+
89+
90+
}

src/main/java/com/mvpjava/Main.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.mvpjava;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Main {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Main.class, args);
11+
}
12+
}

src/main/java/com/mvpjava/Role.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.mvpjava;
2+
3+
public class Role {
4+
5+
String roleName ;
6+
7+
public Role(String roleName) {
8+
this.roleName = roleName;
9+
}
10+
11+
public Role() {
12+
this("minion");
13+
}
14+
15+
public String getRoleName() {
16+
return roleName;
17+
}
18+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
employee.max.salary=100000
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.mvpjava.extensions;
2+
3+
/*
4+
* Copyright 2015-2016 the original author or authors.
5+
*
6+
* All rights reserved. This program and the accompanying materials are
7+
* made available under the terms of the Eclipse Public License v1.0 which
8+
* accompanies this distribution and is available at
9+
*
10+
* http://www.eclipse.org/legal/epl-v10.html
11+
*/
12+
13+
14+
import static org.mockito.Mockito.mock;
15+
16+
import java.lang.reflect.Parameter;
17+
18+
import org.junit.jupiter.api.extension.ExtensionContext;
19+
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
20+
import org.junit.jupiter.api.extension.ExtensionContext.Store;
21+
import org.junit.jupiter.api.extension.ParameterContext;
22+
import org.junit.jupiter.api.extension.ParameterResolver;
23+
import org.junit.jupiter.api.extension.TestInstancePostProcessor;
24+
import org.mockito.Mock;
25+
import org.mockito.MockitoAnnotations;
26+
27+
/**
28+
* {@code MockitoExtension} showcases the {@link TestInstancePostProcessor}
29+
* and {@link ParameterResolver} extension APIs of JUnit 5 by providing
30+
* dependency injection support at the field level and at the method parameter
31+
* level via Mockito 2.x's {@link Mock @Mock} annotation.
32+
*
33+
* @since 5.0
34+
*
35+
* Copied this directly from https://github.com/junit-team/junit5-samples/tree/master/junit5-mockito-extension
36+
* to simplify demo since not a downloadable artifact at this time. Shout out to
37+
* Marc Phillipp for this (https://github.com/marcphilipp)
38+
*/
39+
public class MockitoExtension implements TestInstancePostProcessor, ParameterResolver {
40+
41+
@Override
42+
public void postProcessTestInstance(Object testInstance, ExtensionContext context) {
43+
MockitoAnnotations.initMocks(testInstance);
44+
}
45+
46+
@Override
47+
public boolean supports(ParameterContext parameterContext, ExtensionContext extensionContext) {
48+
return parameterContext.getParameter().isAnnotationPresent(Mock.class);
49+
}
50+
51+
@Override
52+
public Object resolve(ParameterContext parameterContext, ExtensionContext extensionContext) {
53+
return getMock(parameterContext.getParameter(), extensionContext);
54+
}
55+
56+
private Object getMock(Parameter parameter, ExtensionContext extensionContext) {
57+
Class<?> mockType = parameter.getType();
58+
Store mocks = extensionContext.getStore(Namespace.create(MockitoExtension.class, mockType));
59+
String mockName = getMockName(parameter);
60+
61+
if (mockName != null) {
62+
return mocks.getOrComputeIfAbsent(mockName, key -> mock(mockType, mockName));
63+
}
64+
else {
65+
return mocks.getOrComputeIfAbsent(mockType.getCanonicalName(), key -> mock(mockType));
66+
}
67+
}
68+
69+
private String getMockName(Parameter parameter) {
70+
String explicitMockName = parameter.getAnnotation(Mock.class).name().trim();
71+
if (!explicitMockName.isEmpty()) {
72+
return explicitMockName;
73+
}
74+
else if (parameter.isNamePresent()) {
75+
return parameter.getName();
76+
}
77+
return null;
78+
}
79+
80+
}

0 commit comments

Comments
 (0)