Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

37%
+1 −3
Q&A Mocking methods with arguments

Let's say I have this class I want to mock class A { public void add(T arg) { B b = A.getB(); U val = somefunc(arg); V ret = b.add(val); } } I have a spy o...

3 answers  ·  posted 3y ago by klutt‭  ·  last activity 3y ago by klutt‭

#3: Post edited by user avatar Alexei‭ · 2022-02-25T15:50:12Z (almost 3 years ago)
added relevant tag
#2: Post edited by user avatar klutt‭ · 2022-02-25T14:47:28Z (almost 3 years ago)
  • Let's say I have this class I want to mock
  • class A {
  • protected B b;
  • public void add(T arg) {
  • U val = somefunc(arg);
  • V ret = b.add(val);
  • }
  • }
  • I have a spy on A and a mock on B, but when A::foo calls B::bar, I want it to be mocked. But I want some stuff to be done inside it. Something like this:
  • class BMock {
  • List<U> ulist = new ArrayList<>();
  • R add(U u) {
  • if(ulist.contains(u)) throw exception;
  • }
  • return new R(x);
  • }
  • But I'm having a bit of a problem how to do this. I'm looking for something on this form:
  • A a = spy(A.class);
  • B b = mock(B.class);
  • a.b = b;
  • BMock bm = new BMock();
  • doAnswer(bm.add(arg)).when(b).add(any());
  • ^ |
  • | |
  • ------------------
  • What I want to do is to send the any argument over to bm.add
  • In the real code, B::add will contact a remote server to add an item. If it gets called twice with the same item, the server will return an error. A::add will analyze this return code and act accordingly. And this is what I'm trying to simulate. How should I do it?
  • Let's say I have this class I want to mock
  • class A {
  • public void add(T arg) {
  • B b = A.getB();
  • U val = somefunc(arg);
  • V ret = b.add(val);
  • }
  • }
  • I have a spy on A and a mock on B, but when A::foo calls B::bar, I want it to be mocked. But I want some stuff to be done inside it. Something like this:
  • class BMock {
  • List<U> ulist = new ArrayList<>();
  • R add(U u) {
  • if(ulist.contains(u)) throw exception;
  • }
  • return new R(x);
  • }
  • But I'm having a bit of a problem how to do this. I'm looking for something on this form:
  • A a = spy(A.class);
  • B b = mock(B.class);
  • a.b = b;
  • BMock bm = new BMock();
  • doAnswer(bm.add(arg)).when(b).add(any());
  • ^ |
  • | |
  • ------------------
  • What I want to do is to send the any argument over to bm.add
  • In the real code, B::add will contact a remote server to add an item. If it gets called twice with the same item, the server will return an error. A::add will analyze this return code and act accordingly. And this is what I'm trying to simulate. How should I do it?
  • ### EDIT
  • I got some suggestions in comment section. But I might add that I cannot change B because it's an external library, and the class B both misses default constructor and is final.
#1: Initial revision by user avatar klutt‭ · 2022-02-25T11:35:04Z (almost 3 years ago)
Mocking methods with arguments
Let's say I have this class I want to mock

    class A {
        protected B b;

        public void add(T arg) {
            U val = somefunc(arg);
            V ret = b.add(val);
        }
    }

I have a spy on A and a mock on B, but when A::foo calls B::bar, I want it to be mocked. But I want some stuff to be done inside it. Something like this:

    class BMock {
        List<U> ulist = new ArrayList<>();

        R add(U u) {
            if(ulist.contains(u)) throw exception;
        }

        return new R(x);
    }

But I'm having a bit of a problem how to do this. I'm looking for something on this form:

    A a = spy(A.class);
    B b = mock(B.class);
    a.b = b;
    BMock bm = new BMock();
    doAnswer(bm.add(arg)).when(b).add(any());
                     ^                  |
                     |                  |
                      ------------------

What I want to do is to send the any argument over to bm.add




In the real code, B::add will contact a remote server to add an item. If it gets called twice with the same item, the server will return an error. A::add will analyze this return code and act accordingly. And this is what I'm trying to simulate. How should I do it?