Skip to content

Commit

Permalink
Inited basic project structure for Mockito practice.
Browse files Browse the repository at this point in the history
  • Loading branch information
MewX committed Feb 4, 2018
1 parent d8003c6 commit e08b44a
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 0 deletions.
15 changes: 15 additions & 0 deletions JustPractice/java/javamockito/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
group 'javamockito'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'org.mockito', name: 'mockito-all', version: '1.8.4'
}
2 changes: 2 additions & 0 deletions JustPractice/java/javamockito/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rootProject.name = 'javamockito'

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.mewx.practice.javamockito;

import java.util.ArrayList;

public class Main {
public static void main(String[] args) {
ArrayList<String> input = new ArrayList<>();
input.add("One message");

Message messageHost = new Message();
new ServiceCaller().callService(input, messageHost);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.mewx.practice.javamockito;

import java.util.ArrayList;

public class Message {
private ArrayList<String> messages = new ArrayList<>();

public void addMessage(String msg) {
messages.add(msg);
}

public int getSize() {
return messages.size();
}

public String getMessageAt(int idx) {
return idx < messages.size() ? messages.get(idx) : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.mewx.practice.javamockito;

import java.util.List;

/**
* The supposed rule is when the size of the list is less than 2, add a message to message
*/
public class Service {
static final String ERROR_MSG = "This is an error message";

public void runService(List<String> list, Message message) {
final int THRESHOLD = 2;
if (list.size() < THRESHOLD) {
message.addMessage(ERROR_MSG);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.mewx.practice.javamockito;

import java.util.List;

public class ServiceCaller {
public void callService(List<String> list, Message messageHost) {
new Service().runService(list, messageHost);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.mewx.practice.javamockito;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class MessageTest {
private Message message;

@Before
public void setUp() {
message = new Message();
}

@Test
public void addMessage() {
assertEquals(0, message.getSize());
message.addMessage("new message");
assertEquals(1, message.getSize());
}

@Test
public void getMessageAt() {
final String NEW_MSG = "new message";
assertEquals(null, message.getMessageAt(0));
message.addMessage(NEW_MSG);
assertEquals(NEW_MSG, message.getMessageAt(0));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.mewx.practice.javamockito;

import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;

import static org.junit.Assert.*;

public class ServiceTest {
private Service service;
private Message message;
private ArrayList<String> list;

@Before
public void setUp() {
service = new Service();
message = new Message();
list = new ArrayList<>();
}

@Test
public void runServiceWithNoneInList() {
service.runService(list, message);
assertEquals(1, message.getSize());
assertEquals(Service.ERROR_MSG, message.getMessageAt(0));
}

@Test
public void runServiceWithOneInList() {
list.add("One");

service.runService(list, message);
assertEquals(1, message.getSize());
assertEquals(Service.ERROR_MSG, message.getMessageAt(0));
}

@Test
public void runServiceWithTwoInList() {
list.add("One");
list.add("Two");

service.runService(list, message);
assertEquals(0, message.getSize());
}
}

0 comments on commit e08b44a

Please sign in to comment.