Merge pull request #15357 from sam-gardner/BAEL-7240-Verify-Lambda-call-using-mockito
BAEL-7240 Add example code for verifying lambda calls with mockito
This commit is contained in:
commit
238c7d61fb
|
@ -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