Last active
December 20, 2015 11:39
-
-
Save dragester/6125310 to your computer and use it in GitHub Desktop.
Usage example of org.mockito.ArgumentCaptor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.drage.tutorial.mocking; | |
public class CarSelector { | |
private final CarBuilderService service; | |
public CarSelector(CarBuilderService service) { | |
this.service = service; | |
} | |
public void buyRedFerrari(){ | |
BuyingCriteria criteria = new BuyingCriteria(); | |
criteria.setColor("RED"); | |
criteria.setModel("FERRARI"); | |
service.buildCar(criteria); | |
} | |
public void buyPinkCaddilac(){ | |
BuyingCriteria criteria = new BuyingCriteria(); | |
criteria.setColor("HOT PINK"); | |
criteria.setModel("CADDILAC"); | |
service.buildCar(criteria); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.drage.tutorial.mocking; | |
// imports ... | |
@RunWith(MockitoJUnitRunner.class) | |
public class CarSelectorTest { | |
private CarSelector carSelector; | |
@Mock | |
private CarBuilderService service; | |
@Before | |
public void initTest(){ | |
carSelector = new CarSelector(service); | |
} | |
@Test | |
public void testBuyRedFerrari() { | |
carSelector.buyRedFerrari(); | |
ArgumentCaptor<BuyingCriteria> argumentCaptor = ArgumentCaptor.forClass(BuyingCriteria.class); | |
Mockito.verify(service).buildCar(argumentCaptor.capture()); | |
BuyingCriteria buyingCriteria = argumentCaptor.getValue(); | |
assertEquals("RED", buyingCriteria.getColor()); | |
assertEquals("FERRARI", buyingCriteria.getModel()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.drage.tutorial.mocking; | |
public interface CarBuilderService { | |
void buildCar(BuyingCriteria criteria); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.drage.tutorial.mocking; | |
public class BuyingCriteria { | |
private String color; | |
private String model; | |
// getters and setters | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment