Use Mockito in Junit

Mockito

Book类

package testMockito;

import java.util.List;

public class Book {

    private String isbn;
    private String title;
    private List<String> authors;
    private String publication;
    private Integer yearOfPublication;
    private Integer numberOfPages;
    private String image;

    public Book(String isbn, String title, List<String> authors,
            String publication, Integer yearOfPublication,
            Integer numberOfPages, String image) {

        this.isbn = isbn;
        this.title = title;
        this.authors = authors;
        this.publication = publication;
        this.yearOfPublication = yearOfPublication;
        this.numberOfPages = numberOfPages;
        this.image = image;

    }

    public String getIsbn() {
        return isbn;
    }

    public String getTitle() {
        return title;
    }

    public List<String> getAuthors() {
        return authors;
    }

    public String getPublication() {
        return publication;
    }

    public Integer getYearOfPublication() {
        return yearOfPublication;
    }

    public Integer getNumberOfPages() {
        return numberOfPages;
    }

    public String getImage() {
        return image;
    }
}

使用Book的BookDAL类

package testMockito;

import java.util.Collections;
import java.util.List;

public class BookDAL {
    private static BookDAL bookDAL = new BookDAL();

    public List<Book> getAllBooks(){
        return Collections.EMPTY_LIST;
    }

    public Book getBook(String isbn){
        return null;
    }

    public String addBook(Book book){
        return book.getIsbn();
    }

    public String updateBook(Book book){
        return book.getIsbn();
    }

    public static BookDAL getInstance(){
        return bookDAL;
    }

}

对BookDAL进行Junit测试

package testMockito;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.List;

import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class BookDALTest {

    private static BookDAL mockedBookDAL;
    private static Book book1;
    private static Book book2;

    @BeforeClass
    public static void setUp() {
        // create mock object of BookDAL
        mockedBookDAL = mock(BookDAL.class);

        // create some instances of Book class.
        book1 = new Book("8131721019", "Compilers Principles", Arrays.asList(
                "D. Jeffrey Ulman", "Ravi Sethi", "Alfred V. Aho",
                "Monica S. Lam"), "Pearson Education Singapore Pte Ltd", 2008,
                1009, "BOOK_IMAGE");

        book2 = new Book("9788183331630", "Let Us C 13th Edition",
                Arrays.asList("Yashavant Kanetkar"), "BPB PUBLICATIONS", 2012,
                675, "BOOK_IMAGE");

        // Stubbing the methods of mocked BookDAL with mocked data.
        when(mockedBookDAL.getAllBooks()).thenReturn(
                Arrays.asList(book1, book2));
        when(mockedBookDAL.getBook("8131721019")).thenReturn(book1);
        when(mockedBookDAL.addBook(book1)).thenReturn(book1.getIsbn());
        when(mockedBookDAL.updateBook(book1)).thenReturn(book1.getIsbn());
        when(mockedBookDAL.updateBook(book2)).thenReturn(book2.getIsbn());
    }

    @Test
    public void testGetAllBooks() {
        List<Book> allBooks = mockedBookDAL.getAllBooks();
        assertEquals(2, allBooks.size());
        Book myBook = allBooks.get(0); // book1
        assertEquals("8131721019", myBook.getIsbn());
        assertEquals("Compilers Principles", myBook.getTitle());
        assertEquals(4, myBook.getAuthors().size());
        assertEquals((Integer) 2008, myBook.getYearOfPublication());
        assertEquals((Integer) 1009, myBook.getNumberOfPages());
        assertEquals("Pearson Education Singapore Pte Ltd",
                myBook.getPublication());
        assertEquals("BOOK_IMAGE", myBook.getImage());
    }

    @Test
    public void testGetBook() {
        String isbn = "8131721019";

        Book myBook = mockedBookDAL.getBook(isbn);

        assertNotNull(myBook);
        assertEquals(isbn, myBook.getIsbn());
        assertEquals("Compilers Principles", myBook.getTitle());
        assertEquals(4, myBook.getAuthors().size());
        assertEquals("Pearson Education Singapore Pte Ltd",
                myBook.getPublication());
        assertEquals((Integer) 2008, myBook.getYearOfPublication());
        assertEquals((Integer) 1009, myBook.getNumberOfPages());
    }

    @Test
    public void testAddBook() {
        String isbn = mockedBookDAL.addBook(book1);
        assertNotNull(isbn);
        assertEquals(book1.getIsbn(), isbn);

    }

    @Test
    public void testUpdateBook() {
        String isbn = mockedBookDAL.updateBook(book1);
        assertNotNull(isbn);
        assertEquals(book1.getIsbn(), isbn);
        isbn = mockedBookDAL.updateBook(book2);
        assertNotNull(isbn);
        assertEquals(book2.getIsbn(), isbn);

    }

    @Ignore@Test
    public void testGetInstance() {
        fail("Not yet implemented");
    }

}

通过使用mock框架,在不用实际配置数据源的情况下,就可以测试BookDAL类。

Sure, here's an example of how you can use Mockito to write a unit test for a Kafka producer: ```java import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.util.Properties; import org.apache.kafka.clients.producer.KafkaProducer; import org.apache.kafka.clients.producer.ProducerRecord; import org.junit.Before; import org.junit.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; public class KafkaProducerUnitTest { @Mock private KafkaProducer<String, String> kafkaProducer; @InjectMocks private KafkaService kafkaService = new KafkaService(); @Before public void setUp() { MockitoAnnotations.initMocks(this); } @Test public void testSendMessage() { Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); when(kafkaService.createProducer(props)).thenReturn(kafkaProducer); String topic = "test-topic"; String message = "Hello, Kafka!"; kafkaService.sendMessage(topic, message); verify(kafkaProducer, times(1)).send(new ProducerRecord<>(topic, message)); } } ``` In this example, we have a `KafkaService` class that wraps the Kafka producer, and we want to test its `sendMessage` method. We use Mockito to mock the Kafka producer and inject it into the `KafkaService`, and then we use the `when` method to specify the behavior of the `createProducer` method. In the test method, we call the `sendMessage` method with a topic and a message, and then we verify that the `send` method of the Kafka producer is called once with the correct arguments. Note that you'll need to add the Mockito and Kafka dependencies to your project for this example to work.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值