# Typical dependency injectionpublic class DatabaseTransactionLogPRovider implements Provider<TransactionLog> { @Inject Connection connection; public TransactionLog get() { return new DatabaseTransactionLog(connection); }}# FactoryModuleBuilder generates factory using your interfacepublic interface PaymentFactory { Payment create(Date startDate, Money amount); } |
OkHttpClient client = new OkHttpClient();String run(String url) throws IOException { Request request = new Request.Builder() .url(url) .build(); Response response = client.newCall(request).execute(); return response.body().string();} |
public interface GitHubService { @GET("users/{user}/repos") Call<List<Repo>listRepos(@Path("user") String user);} |
Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/") .build(); GitHubService service = retrofit.create(GitHubService.class); |
Call<List<Repo>> repos = service.listRepos("octocat"); |
Flowable.fromCallable(() -{ Thread.sleep(1000); // imitate expensive computation return "Done"; }) .subscribeOn(Schedulers.io()) .observeOn(Schedulers.single()) .subscribe(System.out::println, Throwable::printStackTrace); Thread.sleep(2000); // <--- wait for the flow to finish |
// Define your listenerclass SimpleFileListener{ @Handler public void handle(File msg){ // do something with the file }}// somewhere else in your codeMBassador bus = new MBassador();Object listener = new SimpleFileListener();bus.subscribe (listener);bus.post(new File("/tmp/smallfile.csv")).now();bus.post(new File("/tmp/bigfile.csv")).asynchronously(); |
@Test @Parameters({"17, false", "22, true" }) public void personIsAdult(int age, boolean valid) throws Exception { assertThat(new Person(age).isAdult(), is(valid)); } |
//你可以模拟具体的类,而不仅仅是接口 LinkedList mockedList = mock(LinkedList.class); //stubbing when(mockedList.get(0)).thenReturn("first"); when(mockedList.get(1)).thenThrow(new RuntimeException()); //将打印 "first" System.out.println(mockedList.get(0)); //将抛出runtime exception System.out.println(mockedList.get(1)); //将打印 "null" 因为get(999)没有被stubbed System.out.println(mockedList.get(999)); //Although it is possible to verify a stubbed invocation, usually it's just redundant //If your code cares what get(0) returns, then something else breaks (often even before verify() gets executed). //If your code doesn't care what get(0) returns, then it should not be stubbed. Not convinced? See here. verify(mockedList).get(0); |
@RunWith(JukitoRunner.class)public class EmailSystemTest { @Inject EmailSystemImpl emailSystem; Email dummyEmail; @Before public void setupMocks( IncomingEmails incomingEmails, EmailFactory factory) { dummyEmail = factory.createDummy(); when(incomingEmails.count()).thenReturn(1); when(incomingEmails.get(0)).thenReturn(dummyEmail); } @Test public void shouldFetchEmailWhenStarting( EmailView emailView) { // WHEN emailSystem.start(); // THEN verify(emailView).addEmail(dummyEmail); }} |
@Testpublic void updatesCustomerStatus() throws Exception { // Publish an asynchronous event: publishEvent(updateCustomerStatusEvent); // Awaitility让你等待异步操作完成 await().atMost(5, SECONDS).until(customerStatusIsUpdated()); ...} |
class HelloSpockSpec extends spock.lang.Specification { def "length of Spock's and his friends' names"() { expect: name.size() == length where: name | length "Spock" | 5 "Kirk" | 4 "Scotty" | 6 }} |
{ "request": { "method": "GET", "url": "/some/thing" }, "response": { "status": 200, "statusMessage": "Everything was just fine!" }} |
新闻热点
疑难解答