tailieunhanh - Programming Groovy dynamic productivity for the java developer phần 9

Chuyên mục được chỉ hữu ích với Groovy mã. Nó không giúp đỡ để mô hình phương thức được gọi từ bên trong mã Java biên soạn. Cách tiếp cận trọng bạn đã thấy trong mục 16,5 Mocking Thay, trên trang 244 là hữu ích cho cả hai mã Java và Groovy. | Mocking Using ExpandoMetaClass 249 The output from the previous bit of code is a reassuring pass of the test as shown here Time OK 1 test Categories are useful only with Groovy code. it does not help to mock methods called from within compiled Java code. The overriding approach you saw in Section Mocking by Overriding on page 244 is useful for both Java and Groovy code. However the overriding approach can t be used if the class being tested is final. The categories approach shines in this case. Mocking Using ExpandoMetaClass Another way to intercept method calls in Groovy is to use the Expando-MetaClass cf. Section Injecting Methods Using ExpandoMetaClass on page 208 and Section Injecting Methods into Specific Instances on page 212 . You don t have to create a separate class as in the two approaches you ve seen so far. instead create a closure for each method you want to mock and set that into MetaClass for the instance being tested. Let s take a look at an example. Create a separate instance of ExpandoMetaClass for the instance being tested. This MetaClass will carry the mock implementation of collaborator methods. in this example shown in the following code you create a closure for mocking println and set that into an instance of ExpandoMetaClass for ClassWithHeavierDependencies in line number 9. Similarly you create a closure for mocking someAction in line number 10. The advantage of creating an instance of ExpandoMetaClass specifically for the instance under test is that you don t globally affect the metaclass for CodeWith-HeavierDependencies. So if you have other tests the method you mock does not affect them remember to keep the tests isolated from each other . Mocking Using ExpandoMetaClass 250 Download UnitTestingWithGroovy Line 1 import - - class TestUsingExpandoMetaClass extends GroovyTestCase - 5 void testMyMethod - - def result - def emc new .

TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.