@MockClass 不工作
@MockClass is not working
我是 jmockit 的新手,正在尝试执行以下在线示例。
@MockClass 不工作。我的 BookStore 的 getBookTitle() 方法正在调用原始 class 的函数,而不是模拟的 class.
书店class:
public class BookStore {
public String getBookTitle(String isbn){
return BookStoreService.getBookTitle(isbn);
}
}
BookStoreServiceclass:
public class BookStoreService {
public static String getBookTitle(String isbn){
return "Random";
}
}
测试class:
public class BookStoreTest {
private static Map<String, String> bookMap = new HashMap<String, String>(2);
@BeforeClass
public static void setup() {
System.out.println("in setup()");
bookMap.put("0553293354", "Foundation");
bookMap.put("0836220625", "The Far Side Gallery");
}
@MockClass(realClass = BookStoreService.class)
public static class MockBookstoreService {
@Mock
public static String getBookTitle(String isbn) {
System.out.println("in getBookTitle()");
if (bookMap.containsKey(isbn)) {
return bookMap.get(isbn);
} else {
return null;
}
}
}
@Test
public void testGetBookTitle() throws Exception {
System.out.println("in testGetBookTitle()");
final String isbn = "0553293354";
final String expectedTitle = "Foundation";
BookStore store = new BookStore();
String title = store.getBookTitle(isbn);
System.out.println(title); // This prints "Random" instead of "Foundation"
Assert.assertEquals(title, expectedTitle);
}
}
PS: 我正在使用 TestNG
使用 latest stable version of jmockit 你可以这样做:
@BeforeClass
public static void setup() {
System.out.println("in setup()");
bookMap.put("0553293354", "Foundation");
bookMap.put("0836220625", "The Far Side Gallery");
new MockUp<BookStoreService>() {
@Mock
public String getBookTitle(String isbn) {
System.out.println("in getBookTitle()");
if (bookMap.containsKey(isbn)) {
return bookMap.get(isbn);
} else {
return null;
}
}
};
}
删除过时的块:
public static class MockBookstoreService{...}
我是 jmockit 的新手,正在尝试执行以下在线示例。
@MockClass 不工作。我的 BookStore 的 getBookTitle() 方法正在调用原始 class 的函数,而不是模拟的 class.
书店class:
public class BookStore {
public String getBookTitle(String isbn){
return BookStoreService.getBookTitle(isbn);
}
}
BookStoreServiceclass:
public class BookStoreService {
public static String getBookTitle(String isbn){
return "Random";
}
}
测试class:
public class BookStoreTest {
private static Map<String, String> bookMap = new HashMap<String, String>(2);
@BeforeClass
public static void setup() {
System.out.println("in setup()");
bookMap.put("0553293354", "Foundation");
bookMap.put("0836220625", "The Far Side Gallery");
}
@MockClass(realClass = BookStoreService.class)
public static class MockBookstoreService {
@Mock
public static String getBookTitle(String isbn) {
System.out.println("in getBookTitle()");
if (bookMap.containsKey(isbn)) {
return bookMap.get(isbn);
} else {
return null;
}
}
}
@Test
public void testGetBookTitle() throws Exception {
System.out.println("in testGetBookTitle()");
final String isbn = "0553293354";
final String expectedTitle = "Foundation";
BookStore store = new BookStore();
String title = store.getBookTitle(isbn);
System.out.println(title); // This prints "Random" instead of "Foundation"
Assert.assertEquals(title, expectedTitle);
}
}
PS: 我正在使用 TestNG
使用 latest stable version of jmockit 你可以这样做:
@BeforeClass
public static void setup() {
System.out.println("in setup()");
bookMap.put("0553293354", "Foundation");
bookMap.put("0836220625", "The Far Side Gallery");
new MockUp<BookStoreService>() {
@Mock
public String getBookTitle(String isbn) {
System.out.println("in getBookTitle()");
if (bookMap.containsKey(isbn)) {
return bookMap.get(isbn);
} else {
return null;
}
}
};
}
删除过时的块:
public static class MockBookstoreService{...}