re-organize testing modules
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.easymock;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
public class ArticleReader {
|
||||
|
||||
private List<BaeldungArticle> articles;
|
||||
private Iterator<BaeldungArticle> articleIter;
|
||||
|
||||
public ArticleReader() {
|
||||
this(new ArrayList<>());
|
||||
}
|
||||
|
||||
public ArticleReader(List<BaeldungArticle> articles) {
|
||||
this.articles = articles;
|
||||
this.articleIter = this.articles.iterator();
|
||||
}
|
||||
|
||||
public List<BaeldungArticle> ofTopic(String topic) {
|
||||
return articles
|
||||
.stream()
|
||||
.filter(article -> article
|
||||
.title()
|
||||
.contains(topic))
|
||||
.collect(toList());
|
||||
}
|
||||
|
||||
public BaeldungArticle next() {
|
||||
return this.articleIter.next();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.easymock;
|
||||
|
||||
public class BaeldungArticle {
|
||||
|
||||
public static BaeldungArticle simpleArticle(String title, String content) {
|
||||
return new BaeldungArticle(title, content);
|
||||
}
|
||||
|
||||
private String title;
|
||||
private String content;
|
||||
|
||||
private BaeldungArticle(String title, String content) {
|
||||
this.title = title;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public String title() {
|
||||
return this.title;
|
||||
}
|
||||
|
||||
public String content() {
|
||||
return this.content;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.easymock;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class BaeldungReader {
|
||||
|
||||
private ArticleReader articleReader;
|
||||
private IArticleWriter articleWriter;
|
||||
|
||||
public BaeldungReader() {
|
||||
}
|
||||
|
||||
;
|
||||
|
||||
public BaeldungReader(ArticleReader articleReader) {
|
||||
this.articleReader = articleReader;
|
||||
}
|
||||
|
||||
public BaeldungReader(IArticleWriter writer) {
|
||||
this.articleWriter = writer;
|
||||
}
|
||||
|
||||
public BaeldungReader(ArticleReader articleReader, IArticleWriter writer) {
|
||||
this.articleReader = articleReader;
|
||||
this.articleWriter = writer;
|
||||
}
|
||||
|
||||
public BaeldungArticle readNext() {
|
||||
return articleReader.next();
|
||||
}
|
||||
|
||||
public List<BaeldungArticle> readTopic(String topic) {
|
||||
return articleReader.ofTopic(topic);
|
||||
}
|
||||
|
||||
public String write(String title, String content) {
|
||||
return articleWriter.write(title, content);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.easymock;
|
||||
|
||||
public interface IArticleWriter {
|
||||
|
||||
String write(String title, String content);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.easymock;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface IUserService {
|
||||
public boolean addUser(User user);
|
||||
public List<User> findByEmail(String email);
|
||||
public List<User> findByAge(double age);
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package com.baeldung.easymock;
|
||||
|
||||
public class User {
|
||||
private long id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private double age;
|
||||
private String email;
|
||||
|
||||
public User() {
|
||||
super();
|
||||
}
|
||||
|
||||
public User(String firstName, String lastName) {
|
||||
super();
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
public double getAge() {
|
||||
return age;
|
||||
}
|
||||
public void setAge(double age) {
|
||||
this.age = age;
|
||||
}
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(age);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
result = prime * result + ((email == null) ? 0 : email.hashCode());
|
||||
result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
|
||||
result = prime * result + (int) (id ^ (id >>> 32));
|
||||
result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
User other = (User) obj;
|
||||
if (Double.doubleToLongBits(age) != Double.doubleToLongBits(other.age)) {
|
||||
return false;
|
||||
}
|
||||
if (email == null) {
|
||||
if (other.email != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!email.equals(other.email)) {
|
||||
return false;
|
||||
}
|
||||
if (firstName == null) {
|
||||
if (other.firstName != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!firstName.equals(other.firstName)) {
|
||||
return false;
|
||||
}
|
||||
if (id != other.id) {
|
||||
return false;
|
||||
}
|
||||
if (lastName == null) {
|
||||
if (other.lastName != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!lastName.equals(other.lastName)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.introductionjukito;
|
||||
|
||||
public interface Calculator {
|
||||
|
||||
public double add(double a, double b);
|
||||
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.introductionjukito;
|
||||
|
||||
public class ScientificCalculator extends SimpleCalculator {
|
||||
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.introductionjukito;
|
||||
|
||||
public class SimpleCalculator implements Calculator {
|
||||
|
||||
@Override
|
||||
public double add(double a, double b) {
|
||||
return a+b;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.mocks.jmockit;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class AppManager {
|
||||
|
||||
public boolean managerResponse(String question) {
|
||||
return AppManager.isResponsePositive(question);
|
||||
}
|
||||
|
||||
public static boolean isResponsePositive(String value) {
|
||||
if (value == null)
|
||||
return false;
|
||||
int orgLength = value.length();
|
||||
int randomNumber = randomNumber();
|
||||
return orgLength == randomNumber ? true : false;
|
||||
}
|
||||
|
||||
private static int randomNumber() {
|
||||
return new Random().nextInt(7);
|
||||
}
|
||||
|
||||
private static Integer stringToInteger(String num) {
|
||||
return Integer.parseInt(num);
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package org.baeldung.mocks.jmockit;
|
||||
|
||||
public class AdvancedCollaborator {
|
||||
int i;
|
||||
private int privateField = 5;
|
||||
public AdvancedCollaborator(){}
|
||||
public AdvancedCollaborator(String string) throws Exception{
|
||||
i = string.length();
|
||||
}
|
||||
public String methodThatCallsPrivateMethod(int i){
|
||||
return privateMethod() + i;
|
||||
}
|
||||
public int methodThatReturnsThePrivateField(){
|
||||
return privateField;
|
||||
}
|
||||
private String privateMethod(){
|
||||
return "default:";
|
||||
}
|
||||
class InnerAdvancedCollaborator{}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.baeldung.mocks.jmockit;
|
||||
|
||||
public class Collaborator {
|
||||
|
||||
public boolean collaborate(String string){
|
||||
return false;
|
||||
}
|
||||
|
||||
public void receive(boolean bool){
|
||||
//NOOP
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package org.baeldung.mocks.jmockit;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ExpectationsCollaborator {
|
||||
String methodForAny1(String s, int i, Boolean b);
|
||||
void methodForAny2(Long l, List<String> lst);
|
||||
String methodForWith1(String s, int i);
|
||||
void methodForWith2(Boolean b, List<String> l);
|
||||
String methodForNulls1(String s, List<String> l);
|
||||
void methodForNulls2(String s, List<String> l);
|
||||
void methodForTimes1();
|
||||
void methodForTimes2();
|
||||
void methodForTimes3();
|
||||
void methodForArgThat(Object o);
|
||||
String methodReturnsString();
|
||||
int methodReturnsInt();
|
||||
Object methodForDelegate(int i);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package org.baeldung.mocks.jmockit;
|
||||
|
||||
public class Model {
|
||||
public String getInfo() {
|
||||
return "info";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package org.baeldung.mocks.jmockit;
|
||||
|
||||
public class Performer {
|
||||
private Collaborator collaborator;
|
||||
|
||||
public void perform(Model model){
|
||||
boolean value = collaborator.collaborate(model.getInfo());
|
||||
collaborator.receive(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.baeldung.mocks.testCase;
|
||||
|
||||
public class LoginController {
|
||||
|
||||
public LoginService loginService;
|
||||
|
||||
public String login(UserForm userForm) {
|
||||
if (null == userForm) {
|
||||
return "ERROR";
|
||||
} else {
|
||||
boolean logged;
|
||||
|
||||
try {
|
||||
logged = loginService.login(userForm);
|
||||
} catch (Exception e) {
|
||||
return "ERROR";
|
||||
}
|
||||
|
||||
if (logged) {
|
||||
loginService.setCurrentUser(userForm.getUsername());
|
||||
return "OK";
|
||||
} else {
|
||||
return "KO";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// standard setters and getters
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.baeldung.mocks.testCase;
|
||||
|
||||
public class LoginDao {
|
||||
|
||||
public int login(UserForm userForm) {
|
||||
//actual call to a third party library
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.baeldung.mocks.testCase;
|
||||
|
||||
public class LoginService {
|
||||
|
||||
private LoginDao loginDao;
|
||||
|
||||
private String currentUser;
|
||||
|
||||
public boolean login(UserForm userForm) {
|
||||
assert null != userForm;
|
||||
|
||||
int loginResults = loginDao.login(userForm);
|
||||
|
||||
switch (loginResults) {
|
||||
case 1:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void setCurrentUser(String username) {
|
||||
if (null != username) {
|
||||
this.currentUser = username;
|
||||
}
|
||||
}
|
||||
|
||||
public void setLoginDao(LoginDao loginDao) {
|
||||
this.loginDao = loginDao;
|
||||
}
|
||||
|
||||
// standard setters and getters
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.baeldung.mocks.testCase;
|
||||
|
||||
public class UserForm {
|
||||
|
||||
// public access modifiers as only for testing
|
||||
|
||||
public String password;
|
||||
|
||||
public String username;
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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>
|
||||
Reference in New Issue
Block a user