Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Better solution in JMock or other Java framework to have same number of parameters as protected method has

  1. #1
    Junior Member intelrate's Avatar
    Join Date
    Jun 2009
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Arrow Better solution in JMock or other Java framework to have same number of parameters as protected method has

    Here is a question.

    I wanted my JUnit tests to be more modular and found how to mock protected methods for that purpose. Briefly speaking, we cannot mock and control calls of protected method with JDK dynamic proxy because protected methods are not in any interface but we can create MockInterface with mock method under control and call it from overridden protected method to achieve our goal.

    The problem I’m trying to resolve now is that my mock method should have the same number of parameters as protected method I want to mock. It forces me to create several mock methods with different number of parameters which is not elegant. Does anyone know better solution in JMock or other Java framework? Any help would be very appreciated.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: How to mock protected method in Java

    Hello intelrate, welcome to the Java Programming Forums

    That was an interesting read on your blog.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member intelrate's Avatar
    Join Date
    Jun 2009
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to mock protected method in Java

    Quote Originally Posted by JavaPF View Post
    Hello intelrate, welcome to the Java Programming Forums

    That was an interesting read on your blog.
    Thanks JavaPF.

    Just found another solution with CGLIB if interesting. Refactoring sometimes allows to resolve the problem as well.

    Cheers!

Similar Threads

  1. 50 SCJP Mock Exam Questions with Answers
    By JavaPF in forum The Cafe
    Replies: 3
    Last Post: July 6th, 2010, 11:10 AM
  2. [SOLVED] Method declaration in Java
    By mohsendeveloper in forum Object Oriented Programming
    Replies: 4
    Last Post: June 11th, 2009, 03:18 AM
  3. Replies: 2
    Last Post: March 4th, 2009, 06:32 AM
  4. Replies: 0
    Last Post: February 3rd, 2009, 01:15 AM

Tags for this Thread