mock_java 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f127d38cf74c7e09293aae9cb76377d1fa67fb8c
4
- data.tar.gz: 439e1eb3d925050943ba63d3699dcbd2216745d0
3
+ metadata.gz: f4136938c37e7ecdc65c9ec81da3f444c4865218
4
+ data.tar.gz: 5228d377b1022f060c7c04d0f586fa85d7a08d7b
5
5
  SHA512:
6
- metadata.gz: bb35e74b964829b6bdb4f073a87d9a627f01784543bf6fb02a508f0d34606495aa4cad257cb30f4b19d63badf0673ce22a71dd8e4f3d58b743cca7a41bf0f4fc
7
- data.tar.gz: 2437ca360007014082da60758b4bf5382b755a1fdd94ad764a6c345f6ccaa706d046b867a8fe35f126bfc002476563cf4866a408ccb03a63d0a8ce006fd921d7
6
+ metadata.gz: 9f6eea070f5b55252170a14299eb7c246f35f34bb41a370a6fb9132310501a3a5c319305bcf4c854e58dda0427012d1af0e79d0ff97fea00221f7c69a6ff5549
7
+ data.tar.gz: 6583a97842600b5bc6795ce981dc3acb0580167b3929837c7c8313693aa790b5a27795cda50a9d5598de1e07d6711ae7528281d9bdf8c92a7e3c232876129ec7
@@ -0,0 +1,5 @@
1
+ ## 0.2.0
2
+ * Improve the mock for java classes with inheritance
3
+
4
+ ## 0.1.0
5
+ * First release.
data/Rakefile CHANGED
@@ -9,7 +9,10 @@ end
9
9
  desc 'Compile the java classes'
10
10
  task :java_compiler do
11
11
  puts "Compiling java classes"
12
- system('javac spec/java_classes/**/*.java')
12
+ Dir.glob(File.join('spec', 'java_classes','**','*.java')).each do |file|
13
+ file.gsub!('spec/java_classes/', '')
14
+ system("cd spec/java_classes && javac #{file}")
15
+ end
13
16
  end
14
17
 
15
18
  desc 'Run the test on classes'
@@ -37,8 +37,7 @@ module MockJava
37
37
  end
38
38
 
39
39
  def add_ruby_methods_to(child_class)
40
- java_methods = @clazz.java_class.declared_instance_methods
41
- java_methods.collect! { |x| [x.name, x.generic_parameter_types.size]}
40
+ java_methods = load_java_methods @clazz.java_class
42
41
 
43
42
  ruby_methods = java_methods.inject("") do |s, m|
44
43
  method_name = m[0]
@@ -50,7 +49,7 @@ module MockJava
50
49
 
51
50
  def create_ruby_method(child_class, method_name, total_params = 0)
52
51
  if total_params == 0
53
- child_class.class_eval "\n def #{method_name}\n super \nend"
52
+ child_class.class_eval "def #{method_name}; super; end"
54
53
  else
55
54
  joined_params = ''
56
55
  total_params.times do |t|
@@ -58,10 +57,27 @@ module MockJava
58
57
  end
59
58
  joined_params.gsub!(/(.*),$/, '\1')
60
59
 
61
- child_class.class_eval "\n def #{method_name}(#{joined_params}) \n super #{joined_params} \n end"
60
+ child_class.class_eval "def #{method_name}(#{joined_params}); super #{joined_params}; end"
62
61
  end
63
62
  end
64
63
 
64
+ def load_java_methods(java_class, child_java_methods = [])
65
+ java_methods = child_java_methods
66
+
67
+ if java_class.name != "java.lang.Object"
68
+ java_methods = java_class.declared_instance_methods
69
+ java_methods.collect! { |x| [x.name, x.generic_parameter_types.size]}
70
+
71
+ # add the methods from child and parent, remove duplicates
72
+ java_methods = java_methods + child_java_methods
73
+ java_methods.uniq!
74
+
75
+ java_methods = load_java_methods java_class.superclass, java_methods
76
+ end
77
+
78
+ java_methods
79
+ end
80
+
65
81
  end
66
82
 
67
83
  end
@@ -1,3 +1,3 @@
1
1
  module MockJava
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,23 @@
1
+ package test.inheritance;
2
+
3
+ public class A {
4
+
5
+ private StringBuilder output = new StringBuilder();
6
+
7
+ public void methodA() {
8
+ addToOutput("Method A");
9
+ }
10
+
11
+ public void runMethod() {
12
+ hashCode();
13
+ methodA();
14
+ }
15
+
16
+ protected void addToOutput(String description) {
17
+ output.append(description).append(" ");
18
+ }
19
+
20
+ public String output() {
21
+ return output.toString();
22
+ }
23
+ }
@@ -0,0 +1,13 @@
1
+ package test.inheritance;
2
+
3
+ public class B extends A {
4
+
5
+ public void methodB() {
6
+ addToOutput("Method B");
7
+ }
8
+
9
+ public void runMethod() {
10
+ methodA();
11
+ methodB();
12
+ }
13
+ }
@@ -0,0 +1,14 @@
1
+ package test.inheritance;
2
+
3
+ public class C extends B {
4
+
5
+ public void methodC() {
6
+ addToOutput("Method C");
7
+ }
8
+
9
+ public void runMethod() {
10
+ methodA();
11
+ methodB();
12
+ methodC();
13
+ }
14
+ }
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ java_import 'test.inheritance.A'
4
+ java_import 'test.inheritance.B'
5
+ java_import 'test.inheritance.C'
6
+
7
+ describe 'mocking methods on a class with inheritance' do
8
+ context 'mocking the parent class' do
9
+ it 'should mock the method' do
10
+ a = mock_java A
11
+
12
+ expect(a).to receive(:methodA).and_return 'mock A'
13
+ expect(a.methodA).to eq('mock A')
14
+ end
15
+ end
16
+
17
+ context 'mocking the child class' do
18
+ it 'should mock the method' do
19
+ b = mock_java B
20
+
21
+ expect(b).to receive(:methodB).and_return 'mock B'
22
+ expect(b.methodB).to eq('mock B')
23
+ end
24
+ end
25
+
26
+ context 'mocking the grandchild class' do
27
+ it 'should mock the method' do
28
+ c = mock_java C
29
+
30
+ expect(c).to receive(:methodC).and_return 'mock C'
31
+ expect(c.methodC).to eq('mock C')
32
+ end
33
+ end
34
+ end
35
+
36
+ describe 'expecting parents methods to be called on child classes' do
37
+ context 'for parent class' do
38
+ let(:a) { mock_java A }
39
+
40
+ it 'should not see that method from parent Object class was called' do
41
+ expect(a).to_not receive(:hashCode)
42
+ end
43
+
44
+ it 'should see that his method was called' do
45
+ expect(a).to receive(:methodA).and_call_original
46
+
47
+ a.runMethod
48
+ expect(a.output).to eq("Method A ")
49
+ end
50
+ end
51
+
52
+ context 'for child class' do
53
+ let(:b) { mock_java B }
54
+
55
+ it 'should see that parent method was called' do
56
+ expect(b).to receive(:methodA).once
57
+ b.runMethod
58
+ end
59
+
60
+ it 'should see that his method was called' do
61
+ expect(b).to receive(:methodB).and_call_original
62
+
63
+ b.runMethod
64
+ expect(b.output).to eq("Method A Method B ")
65
+ end
66
+ end
67
+
68
+ context 'for grandchild class' do
69
+ let(:c) { mock_java C }
70
+
71
+ it 'should see that grandparent method was called' do
72
+ expect(c).to receive(:methodA).once
73
+ c.runMethod
74
+ end
75
+
76
+ it 'should see that parent method was called' do
77
+ expect(c).to receive(:methodB).once
78
+ c.runMethod
79
+ end
80
+
81
+ it 'should see that his method was called' do
82
+ expect(c).to receive(:methodC).and_call_original
83
+
84
+ c.runMethod
85
+ expect(c.output).to eq("Method A Method B Method C ")
86
+ end
87
+ end
88
+ end
@@ -4,14 +4,11 @@ java_import 'test.PublicMethod'
4
4
 
5
5
  describe 'mocking objects' do
6
6
 
7
- before(:each) do
8
- obj = PublicMethod.new
9
- @mock = mock_java obj
10
- end
11
-
12
7
  it 'should mock the object' do
13
- expect(@mock).to receive(:getPublicMethod).and_return 'mock'
14
- expect(@mock.getPublicMethod).to eq('mock')
8
+ mock = mock_java PublicMethod.new
9
+
10
+ expect(mock).to receive(:getPublicMethod).and_return 'mock'
11
+ expect(mock.getPublicMethod).to eq('mock')
15
12
  end
16
13
 
17
14
  end
@@ -4,29 +4,27 @@ java_import 'test.PrivateMethod'
4
4
 
5
5
  describe 'classes with private methods' do
6
6
 
7
- before(:each) do
8
- @mock = mock_java PrivateMethod
9
- end
7
+ let(:mock) { mock_java PrivateMethod.new }
10
8
 
11
9
  context 'when is changing behaviour' do
12
10
  it 'should mock methods with no parameters' do
13
- expect(@mock).to receive(:getPrivateMethod).and_return 'mock'
14
- expect(@mock.getPrivateMethod).to eq('mock')
11
+ expect(mock).to receive(:getPrivateMethod).and_return 'mock'
12
+ expect(mock.getPrivateMethod).to eq('mock')
15
13
  end
16
14
 
17
15
  it 'should mock methods with 1 parameters' do
18
- expect(@mock).to receive(:getPrivateMethodArg1).and_return 'mock'
19
- expect(@mock.getPrivateMethodArg1 'param1').to eq('mock')
16
+ expect(mock).to receive(:getPrivateMethodArg1).and_return 'mock'
17
+ expect(mock.getPrivateMethodArg1 'param1').to eq('mock')
20
18
  end
21
19
 
22
20
  it 'should mock methods with 2 parameters' do
23
- expect(@mock).to receive(:getPrivateMethodArg2).and_return 'mock'
24
- expect(@mock.getPrivateMethodArg2 'param1', 'param2').to eq('mock')
21
+ expect(mock).to receive(:getPrivateMethodArg2).and_return 'mock'
22
+ expect(mock.getPrivateMethodArg2 'param1', 'param2').to eq('mock')
25
23
  end
26
24
 
27
25
  it 'should mock methods with varargs parameters' do
28
- expect(@mock).to receive(:getPrivateMethodVarargs).and_return 'mock'
29
- output = @mock.getPrivateMethodVarargs %w(param1 param2).to_java
26
+ expect(mock).to receive(:getPrivateMethodVarargs).and_return 'mock'
27
+ output = mock.getPrivateMethodVarargs %w(param1 param2).to_java
30
28
  expect(output).to eq('mock')
31
29
  end
32
30
  end
@@ -34,25 +32,25 @@ describe 'classes with private methods' do
34
32
  context 'when is not changing behaviour' do
35
33
  it 'should raise calling methods with no parameters that are private' do
36
34
  expect {
37
- @mock.getPrivateMethod.to eq('PrivateMethod')
35
+ mock.getPrivateMethod.to eq('PrivateMethod')
38
36
  }.to raise_error NoMethodError, /no superclass method/
39
37
  end
40
38
 
41
39
  it 'should raise calling methods with 1 parameteres that are private' do
42
40
  expect {
43
- @mock.getPrivateMethodArg1 'test1'
41
+ mock.getPrivateMethodArg1 'test1'
44
42
  }.to raise_error NoMethodError, /no superclass method/
45
43
  end
46
44
 
47
45
  it 'should raise calling methods with 2 parameteres that are private' do
48
46
  expect {
49
- @mock.getPrivateMethodArg2 'test1', 'test2'
47
+ mock.getPrivateMethodArg2 'test1', 'test2'
50
48
  }.to raise_error NoMethodError, /no superclass method/
51
49
  end
52
50
 
53
51
  it 'should raise calling methods with varargs params that are private' do
54
52
  expect {
55
- @mock.getPrivateMethodVarargs(%w(param1 param2).to_java(:string))
53
+ mock.getPrivateMethodVarargs(%w(param1 param2).to_java(:string))
56
54
  }.to raise_error NoMethodError, /no superclass method/
57
55
  end
58
56
  end
@@ -6,25 +6,25 @@ describe 'classes with public constructores' do
6
6
 
7
7
  context 'when using no arguments' do
8
8
  it 'should call the parent default constructor' do
9
- @mock = mock_java PublicConstructor
10
- expect(@mock.getMessage).to eq('Default Constructor')
9
+ mock = mock_java PublicConstructor
10
+ expect(mock.getMessage).to eq('Default Constructor')
11
11
  end
12
12
  end
13
13
 
14
14
  context 'when using the init arguments' do
15
15
  it 'should call the parent constructor with 1 parameters for 1 argument' do
16
- @mock = mock_java PublicConstructor, 'mock1'
17
- expect(@mock.getMessage).to eq('Constructor 1: mock1')
16
+ mock = mock_java PublicConstructor, 'mock1'
17
+ expect(mock.getMessage).to eq('Constructor 1: mock1')
18
18
  end
19
19
 
20
20
  it 'should call the parent constructor with 2 parameters for 2 argument' do
21
- @mock = mock_java PublicConstructor, 'mock1', 'mock2'
22
- expect(@mock.getMessage).to eq('Constructor 2: mock1 mock2')
21
+ mock = mock_java PublicConstructor, 'mock1', 'mock2'
22
+ expect(mock.getMessage).to eq('Constructor 2: mock1 mock2')
23
23
  end
24
24
 
25
25
  it 'should call the parent constructor when using varargs' do
26
- @mock = mock_java PublicConstructor, 'mock1', 'mock2', 'mock3'
27
- expect(@mock.getMessage).to eq('Constructor Varargs1: [mock1, mock2, mock3]')
26
+ mock = mock_java PublicConstructor, 'mock1', 'mock2', 'mock3'
27
+ expect(mock.getMessage).to eq('Constructor Varargs1: [mock1, mock2, mock3]')
28
28
  end
29
29
  end
30
30
 
@@ -3,49 +3,47 @@ require 'spec_helper'
3
3
  java_import 'test.PublicMethod'
4
4
 
5
5
  describe 'classes with public methods' do
6
- before(:each) do
7
- @mock = mock_java PublicMethod
8
- end
6
+ let(:mock) { mock_java PublicMethod }
9
7
 
10
8
  context 'when is changing behaviour' do
11
9
  it 'should mock methods with no parameters' do
12
- expect(@mock).to receive(:getPublicMethod).and_return 'mock'
13
- expect(@mock.getPublicMethod).to eq('mock')
10
+ expect(mock).to receive(:getPublicMethod).and_return 'mock'
11
+ expect(mock.getPublicMethod).to eq('mock')
14
12
  end
15
13
 
16
14
  it 'should mock methods with 1 parameters' do
17
- expect(@mock).to receive(:getPublicMethodArg1).and_return 'mock'
18
- expect(@mock.getPublicMethodArg1 'param1').to eq('mock')
15
+ expect(mock).to receive(:getPublicMethodArg1).and_return 'mock'
16
+ expect(mock.getPublicMethodArg1 'param1').to eq('mock')
19
17
  end
20
18
 
21
19
  it 'should mock methods with 2 parameters' do
22
- expect(@mock).to receive(:getPublicMethodArg2).and_return 'mock'
23
- expect(@mock.getPublicMethodArg2 'param1', 'param2').to eq('mock')
20
+ expect(mock).to receive(:getPublicMethodArg2).and_return 'mock'
21
+ expect(mock.getPublicMethodArg2 'param1', 'param2').to eq('mock')
24
22
  end
25
23
 
26
24
  it 'should mock methods with varargs parameters' do
27
- expect(@mock).to receive(:getPublicMethodVarargs).and_return 'mock'
25
+ expect(mock).to receive(:getPublicMethodVarargs).and_return 'mock'
28
26
 
29
- output = @mock.getPublicMethodVarargs %w(param1 param2).to_java
27
+ output = mock.getPublicMethodVarargs %w(param1 param2).to_java
30
28
  expect(output).to eq('mock')
31
29
  end
32
30
  end
33
31
 
34
32
  context 'when is not changing behaviour' do
35
33
  it 'should call the the original methods with no parameters' do
36
- expect(@mock.getPublicMethod).to eq('PublicMethod')
34
+ expect(mock.getPublicMethod).to eq('PublicMethod')
37
35
  end
38
36
 
39
37
  it 'should call original methods with 1 parameters' do
40
- expect(@mock.getPublicMethodArg1 'test').to eq('test')
38
+ expect(mock.getPublicMethodArg1 'test').to eq('test')
41
39
  end
42
40
 
43
41
  it 'should call original methods with 2 parameters' do
44
- expect(@mock.getPublicMethodArg2 'test1', 'test2').to eq('test1 test2')
42
+ expect(mock.getPublicMethodArg2 'test1', 'test2').to eq('test1 test2')
45
43
  end
46
44
 
47
45
  it 'should call original methods with varargs parameters' do
48
- output = @mock.getPublicMethodVarargs(%w(param1 param2).to_java(:string))
46
+ output = mock.getPublicMethodVarargs(%w(param1 param2).to_java(:string))
49
47
  expect(output.to_a).to eq(%w(param1 param2))
50
48
  end
51
49
  end
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mock_java
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frederico Benevides
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-08 00:00:00.000000000 Z
11
+ date: 2015-06-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
15
14
  requirement: !ruby/object:Gem::Requirement
16
15
  requirements:
17
- - - "~>"
16
+ - - ~>
18
17
  - !ruby/object:Gem::Version
19
18
  version: '1.9'
20
- type: :development
19
+ name: bundler
21
20
  prerelease: false
21
+ type: :development
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.9'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rake
29
28
  requirement: !ruby/object:Gem::Requirement
30
29
  requirements:
31
- - - "~>"
30
+ - - ~>
32
31
  - !ruby/object:Gem::Version
33
32
  version: '10.0'
34
- type: :development
33
+ name: rake
35
34
  prerelease: false
35
+ type: :development
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rspec
43
42
  requirement: !ruby/object:Gem::Requirement
44
43
  requirements:
45
- - - "~>"
44
+ - - ~>
46
45
  - !ruby/object:Gem::Version
47
46
  version: '3.2'
48
- type: :development
47
+ name: rspec
49
48
  prerelease: false
49
+ type: :development
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.2'
55
55
  description: mock_java a lib that help to mock java classes in ruby world!
@@ -59,9 +59,10 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - ".gitignore"
63
- - ".rspec"
64
- - ".travis.yml"
62
+ - .gitignore
63
+ - .rspec
64
+ - .travis.yml
65
+ - CHANGELOG.md
65
66
  - Gemfile
66
67
  - LICENSE.txt
67
68
  - README.md
@@ -73,6 +74,10 @@ files:
73
74
  - spec/java_classes/test/PrivateMethod.java
74
75
  - spec/java_classes/test/PublicConstructor.java
75
76
  - spec/java_classes/test/PublicMethod.java
77
+ - spec/java_classes/test/inheritance/A.java
78
+ - spec/java_classes/test/inheritance/B.java
79
+ - spec/java_classes/test/inheritance/C.java
80
+ - spec/mock_java/inherited_method_spec.rb
76
81
  - spec/mock_java/objects_already_created_spec.rb
77
82
  - spec/mock_java/private_method_spec.rb
78
83
  - spec/mock_java/public_constructor_spec.rb
@@ -82,30 +87,34 @@ homepage: http://github.com/fredericobenevides/mock_java
82
87
  licenses:
83
88
  - MIT
84
89
  metadata: {}
85
- post_install_message:
90
+ post_install_message:
86
91
  rdoc_options: []
87
92
  require_paths:
88
93
  - lib
89
94
  required_ruby_version: !ruby/object:Gem::Requirement
90
95
  requirements:
91
- - - ">="
96
+ - - '>='
92
97
  - !ruby/object:Gem::Version
93
98
  version: '0'
94
99
  required_rubygems_version: !ruby/object:Gem::Requirement
95
100
  requirements:
96
- - - ">="
101
+ - - '>='
97
102
  - !ruby/object:Gem::Version
98
103
  version: '0'
99
104
  requirements: []
100
- rubyforge_project:
101
- rubygems_version: 2.4.5
102
- signing_key:
105
+ rubyforge_project:
106
+ rubygems_version: 2.4.6
107
+ signing_key:
103
108
  specification_version: 4
104
109
  summary: Mock java is a lib to mock java classes in the ruby world!
105
110
  test_files:
106
111
  - spec/java_classes/test/PrivateMethod.java
107
112
  - spec/java_classes/test/PublicConstructor.java
108
113
  - spec/java_classes/test/PublicMethod.java
114
+ - spec/java_classes/test/inheritance/A.java
115
+ - spec/java_classes/test/inheritance/B.java
116
+ - spec/java_classes/test/inheritance/C.java
117
+ - spec/mock_java/inherited_method_spec.rb
109
118
  - spec/mock_java/objects_already_created_spec.rb
110
119
  - spec/mock_java/private_method_spec.rb
111
120
  - spec/mock_java/public_constructor_spec.rb