BAEL-7240 Add example code for verifying lambda calls with mockito
This commit is contained in:
parent
67e11e004e
commit
75afba12d1
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.lambdacalls;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class LambdaExample {
|
||||
|
||||
private BrickLayer brickLayer = new BrickLayer();
|
||||
|
||||
static ArrayList<String> bricksList = new ArrayList<>();
|
||||
|
||||
public void createWall(String bricks){
|
||||
Runnable build = () -> brickLayer.layBricks(bricks);
|
||||
build.run();
|
||||
}
|
||||
|
||||
public ArrayList<String> getBricksList(){
|
||||
return bricksList;
|
||||
}
|
||||
|
||||
static class BrickLayer {
|
||||
void layBricks(String bricks){
|
||||
bricksList.add(bricks);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.lambdacalls;
|
||||
|
||||
import static com.baeldung.lambdacalls.LambdaExample.*;
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class LambdaCallsUnitTest {
|
||||
|
||||
@Mock
|
||||
private/**/ BrickLayer brickLayer;
|
||||
@InjectMocks
|
||||
private LambdaExample lambdaExample;
|
||||
|
||||
@Test
|
||||
public void whenCallingALambda_thenTheInvocationCanBeConfirmedWithCorrectArguments(){
|
||||
String bricks = "red bricks";
|
||||
lambdaExample.createWall(bricks);
|
||||
verify(brickLayer).layBricks(bricks);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallingALambda_thenCorrectBehaviourIsPerformed(){
|
||||
LambdaExample lambdaExample = new LambdaExample();
|
||||
String bricks = "red bricks";
|
||||
|
||||
lambdaExample.createWall(bricks);
|
||||
ArrayList<String> bricksList = lambdaExample.getBricksList();
|
||||
|
||||
assertEquals(bricks, bricksList.get(0));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue