Junit模拟JNDI数据源

一、Junit4+spring-test测试web工程Controller层功能

1、新建测试基类,指定配置文件路径

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:spring-*.xml" })
public class BaseTest {

}
2、新建controller测试类,继承测试基类

import static org.junit.Assert.*;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.servlet.ModelAndView;

import com.fit.BaseTest;

public class UserControllerTest extends BaseTest {

	@Autowired
	private UserController controller;

	@Test
	public void testLoginCheck() {
		//模拟HttpServletRequest、HttpServletResponse
		MockHttpServletRequest request = new MockHttpServletRequest();
		request.addParameter("id", "18");
		request.addParameter("name", "jack");
		MockHttpServletResponse response = new MockHttpServletResponse();
		//
		ModelAndView mv = controller.loginCheck(request, response);
		assertEquals("main", mv.getViewName());
	}
}
3、Run As Junit Test

二、Junit4+spring-test模拟JNDI数据源

1、新建测试基类,指定配置文件,模拟jndi数据源

import javax.sql.DataSource;

import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.springframework.mock.jndi.SimpleNamingContextBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:spring-*.xml" })
public class BaseTest {

	@BeforeClass
	public void init() {
		DataSource ds = create a datasource;
		SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
		builder.bind("jndi-datasource-name", ds);
		builder.activate();
	}
}
2、新建web功能测试类

同上

3、Run As Junit Test


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于模拟 Jackson 的 JsonParseException,您可以使用 JUnit 和 Mockito 来进行模拟测试。下面是一个示例代码: 首先,您需要导入所需的依赖项(JUnit、Mockito 和 Jackson): ```xml <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>3.12.4</version> <scope>test</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.13.0</version> </dependency> ``` 然后,您可以编写一个测试类来模拟 JsonParseException: ```java import com.fasterxml.jackson.core.JsonParseException; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import java.io.IOException; import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class JsonParseExceptionTest { @Mock private JsonParseException jsonParseException; @Test(expected = JsonParseException.class) public void testJsonParseException() throws IOException { when(jsonParseException.getMessage()).thenReturn("Invalid JSON"); throw jsonParseException; } } ``` 在上面的示例中,我们使用了 Mockito 来创建一个模拟的 JsonParseException 对象,并通过 `when` 方法指定了当调用 `getMessage` 方法时返回的消息内容。然后,通过抛出该模拟对象来模拟 JsonParseException 的发生。 最后,使用 `@RunWith(MockitoJUnitRunner.class)` 注解来运行测试,并使用 `@Test(expected = JsonParseException.class)` 注解来指定期望抛出 JsonParseException 异常。 这样,当执行测试时,将会抛出模拟的 JsonParseException 异常,并且其消息内容为 "Invalid JSON"。 希望能帮到您!如果您有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值