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
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...
#2: Post edited
- 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
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?