Last active
April 14, 2020 09:59
-
-
Save mtov/c162dce743cc2cb8fdbc25605c35cc2b to your computer and use it in GitHub Desktop.
Mock manual
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
public class Book { | |
private String titulo; | |
public Book(String titulo) { | |
this.titulo = titulo; | |
} | |
public String getTitulo() { | |
return titulo; | |
} | |
} |
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
import org.json.JSONObject; | |
public class BookSearch { | |
BookService rbs; | |
public BookSearch(BookService rbs) { | |
this.rbs = rbs; | |
} | |
public Book getBook(int isbn) { | |
String json = rbs.search(isbn); | |
JSONObject obj = new JSONObject(json); | |
String titulo = (String) obj.get("titulo"); | |
return new Book(titulo); | |
} | |
} |
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
import static org.junit.Assert.*; | |
import org.junit.*; | |
class BookConst { | |
public static final String ESM = "{ \"titulo\": \"Eng Soft Moderna\" }"; | |
public static final String NULLBOOK = "{ \"titulo\": \"NULL\" }"; | |
} | |
class MockBookService implements BookService { | |
public String search(int isbn) { | |
if (isbn == 1234) | |
return BookConst.ESM; | |
return BookConst.NULLBOOK; | |
} | |
} | |
public class BookSearchTest { | |
BookService service1; | |
@Before | |
public void init() { | |
service1 = new MockBookService(); | |
} | |
@Test | |
public void testGetBook() { | |
BookSearch bs = new BookSearch(service1); | |
String titulo = bs.getBook(1234).getTitulo(); | |
assertEquals("Eng Soft Moderna", titulo); | |
} | |
} | |
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
public interface BookService { | |
String search(int isbn); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment