LoginSignup
0
0

Mockitoでメソッド内のobjectMapper.writeValueAsStringの例外は発生させる

Posted at

convertToJson.java

package com.example.helloworld3.app.welcome;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonConverter {

    public String convertToJson(Object object) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(object);
    }
}

abcTest.java

package com.example.helloworld3.app.welcome;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

//import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import static org.junit.Assert.*;

import org.junit.Test;

public class abcTest {

    @Test
    public void testConvertToJsonThrowsException() {
        JsonConverter jsonConverter = new JsonConverter();
        
        // Test that calling convertToJson throws JsonProcessingException
        assertThrows(JsonProcessingException.class, () -> jsonConverter.convertToJson(new Object()));
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0