JUNIT测试时ContextLoader.getCurrentWebApplicationContext()=NULL

在JUNIT测试方法中加入以下代码

MockServletContext sc = new MockServletContext("");
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/applicationContext.xml");
ServletContextListener listener = new ContextLoaderListener();
ServletContextEvent event = new ServletContextEvent(sc);
listener.contextInitialized(event);
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个可能的 JUnit 白盒测试代码示例: ```java package model; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import org.junit.Before; import org.junit.Test; public class TypeTest { private Type type; @Before public void setUp() throws Exception { type = new Type(); } @Test public void testGetSetId() { int expected = 1; type.setId(expected); int actual = type.getId(); assertEquals(expected, actual); } @Test public void testGetSetName() { String expected = "Type Name"; type.setName(expected); String actual = type.getName(); assertEquals(expected, actual); } @Test public void testGetSetEncodeName() { String expected = "Type+Name"; type.setEncodeName(expected); String actual = type.getEncodeName(); assertEquals(expected, actual); } @Test public void testConstructorWithIdAndName() { int expectedId = 1; String expectedName = "Type Name"; type = new Type(expectedId, expectedName); assertNotNull(type); assertEquals(expectedId, type.getId()); assertEquals(expectedName, type.getName()); } @Test public void testConstructorWithName() { String expectedName = "Type Name"; type = new Type(expectedName); assertNotNull(type); assertEquals(expectedName, type.getName()); assertNotNull(type.getEncodeName()); } @Test(expected = UnsupportedEncodingException.class) public void testSetNameWithUnsupportedEncodingException() throws UnsupportedEncodingException { String invalidName = "Type Name"; type.setName(invalidName); type.setEncodeName(null); // Reset the encoded name // Force a UTF-8 encoding error by passing an invalid character type.setName(invalidName + "\uD83D"); // Invalid Unicode character } } ``` 该测试类检查以下方面: - 检查 getId() 和 setId() 方法是否正常工作。 - 检查 getName() 和 setName() 方法是否正常工作,以及是否在设置名称正确地对其进行编码。 - 检查 getEncodeName() 和 setEncodeName() 方法是否正常工作。 - 检查带有 id 和 name 参数的构造函数是否正常工作。 - 检查带有 name 参数的构造函数是否正常工作,并且在名称设置是否始终生成编码名称。 - 检查当使用不支持的编码是否会抛出 UnsupportedEncodingException 异常。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值