Tag: unit testing

弹簧mvc控制器的unit testing,整数值为@RequestParam

我有以下控制器接受输入为@RequestParam @RequestMapping(value = “/fetchstatus”, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public Response fetchStatus( @RequestParam(value = “userId”, required = true) Integer userId) { Response response = new Response(); try { response.setResponse(service.fetchStatus(userId)); response = (Response) Util.getResponse( response, ResponseCode.SUCCESS, FETCH_STATUS_SUCCESS, Message.SUCCESS); } catch (NullValueException e) { e.printStackTrace(); response = (Response) Util.getResponse( response, ResponseCode.FAILED, e.getMessage(), Message.ERROR); } catch (Exception […]

使用Mockito 2.0.7模拟lambda表达式

我想模拟我的存储库上提供的查询,如下所示: @Test public void GetByEmailSuccessful() { // setup mocks Mockito.when(this.personRepo.findAll() .stream() .filter(p -> (p.getEmail().equals(Mockito.any(String.class)))) .findFirst() .get()) .thenReturn(this.personOut); Mockito.when(this.communityUserRepo.findOne(this.communityUserId)) .thenReturn(this.communityUserOut); … 我的@Before方法如下所示: @Before public void initializeMocks() throws Exception { // prepare test data. this.PrepareTestData(); // init mocked repos. this.personRepo = Mockito.mock(IPersonRepository.class); this.communityUserRepo = Mockito.mock(ICommunityUserRepository.class); this.userProfileRepo = Mockito.mock(IUserProfileRepository.class); } 可悲的是,当我运行测试时,我收到错误: java.util.NoSuchElementException:没有值存在 当我双击错误时,它指向第一个lambda的.get()方法。 有没有人成功嘲笑过一个lambda表达式,知道如何解决我的问题?

截断所有表的简单方法,清除一级和二级hibernate缓存?

我正在为我正在研究的S​​pring / Hibernate应用程序编写一些集成测试,我想尽可能接近真实条件进行测试,包括使用Hibernate的二级缓存和提交事务。 我想知道是否有一种有效的方法让Hibernate从数据库和缓存中删除所有内容。 我能想到的最好的就是为每种类型的对象使用HQL“从XImpl删除”行,但我有几十个域对象,感觉应该有更好的方法。

使用属性值的RequestMapping进行Spring Boot REST控制器测试

关于Spring Boot REST Controller的unit testing,我遇到了@RequestMapping和应用程序属性的问题。 @RestController @RequestMapping( “${base.url}” ) public class RESTController { @RequestMapping( value = “/path/to/{param}”, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE ) public String getStuff( @PathVariable String param ) { // implementation of stuff } } 我正在处理应用程序的几个配置文件,因此我有几个application-{profile}.properties文件。 在每个文件中, base.url属性值已设置并存在。 我还有一个不同的Spring Context配置用于测试,只有一个Bean与高效版本不同。 使用JUNit和Mockito / RestAssured,我的unit testing如下所示: @ActiveProfiles( “dev” ) @RunWith( SpringJUnit4ClassRunner.class ) @SpringApplicationConfiguration( classes […]

Mockito有局部变量

我有一个返回String的简单方法。 它还会创建一个本地List 。 我想测试添加到本地List的值。 这是一个例子 package com.impl; import java.util.ArrayList; import java.util.List; import com.test.domain.CustomerVo; public class ClassImpl { public String assignGift(CustomerVo customerVo) { List listOfGift = new ArrayList(); if (customerVo.getName().equals(“Joe”)) { listOfGift.add(“ball”); } else if ((customerVo.getName().equals(“Terry”))) { listOfGift.add(“car”); } else if (customerVo.getName().equals(“Merry”)) { listOfGift.add(“tv”); }else { listOfGift.add(“no gift”); } return “dummyString”; } } 当customerVo.getName.equals(“Terry”) , car被添加到本地List时,如何测试。

Mockito – Mock没有被注入其中一个测试用例

我有一个jsf spring应用程序并使用mockito进行unit testing。 当我在iEmployeeService运行junit测试时,我不断收到NullPointerException 。 iSecurityLoginService没有Exception 。 要嘲笑的方法 @Autowired IEmployeeService iEmployeeService; @Autowired ISecurityLoginService iSecurityLoginService; public void addEvent() { entityEventsCreate.setTitle(entityEventsCreate.getTitle()); entityEventsCreate.setModifiedBy(iSecurityLoginService .findLoggedInUserId()); int eventId = iEmployeeService.addEmployeeTimeOff(entityEventsCreate); } 我的JUnit测试用@RunWith(MockitoJUnitRunner.class)注释 @Mock ISecurityLoginService iSecurityLoginService; @Mock IEmployeeService iEmployeeService; @InjectMocks ServiceCalendarViewBean serviceCalendarViewBean = new ServiceCalendarViewBean(); @Before public void initMocks() { MockitoAnnotations.initMocks(this); } @Test public void testSaveEvent() { Mockito.when(iSecurityLoginService.findLoggedInUserId()).thenReturn(1); serviceCalendarViewBean.getEntityEventsCreate().setTitle(“Junit Event Testing”); […]

TDD时提取类。 如何测试新提取的类?

所以我在我的主类中有几个方法使用矩阵来设置或关闭像素。 我已经运行了所有当前的测试,所以,我已经决定已经抽出一些与矩阵相关的逻辑,并创建一个Matrix类。 我的问题是,除了我目前为我的SUT课程进行的测试(我刚刚开始,所以我目前只有一个课程,SUT的主要课程),我应该为它创建unit testing吗? 如果是这样,你怎么做? 我的意思是,我现在是否让我所有的代码都是现在的,创建makeunit testing逐一进行测试第一种方法,直到我看到我拥有所有function上我想要的只有那里我重构我的代码? 我只是直接创建Matrix类,只是确保旧测试仍然通过,一切都好吗? 谢谢

最终类中的Powermock静态最终方法

我写的测试用例: public class AClassUnderTest { // This test class has a method call public Long methodUnderTest() { // Uses the FinalUtilityClass which contains static final method FinalUtilityClass.myStaticFinalMethod(); // I want to mock above call so that test case for my “methodUnderTest” passes } } 我有一个最后一堂课。 public final class FinalUtilityClass { /** * Method has 3 […]

在eclipse中运行JUnit测试后,我可以以XML格式导出结果。 如何使用JUnit命令行获取该xml文件?

我有Javaunit testing,我在Eclipse中使用JUnit插件运行。 在eclipse中完成所有测试后,我可以将结果导出为.xml格式,我将其用于将结果上传到测试结果跟踪工具中。 现在我在AIX系统中运行相同的测试,我使用命令行来运行JUnit测试。 我的命令是 java org.junit.runner.JUnitCore com.rsa.qa.sae.test.testClassName 现在我想在类似的XML文件中获得这些测试的结果。 我怎么才能得到它?

如何JUnit测试从文件解析的对象

我正在编写一个解析一些文件而不是处理这些文件的程序。 解析器经过了很好的测试,但我也必须测试处理部分。 问题是unit testing函数我需要来自解析器的大对象。 我不想手动创建对象(这是太多的时间)。 我的目标是重构一些过程函数,因为某些函数做了很多事情并且使用了大量的对象。 语言是java,我使用JUnit。 我试图在unit testing中保存对象并加载,但这是唯一的方法吗? 有什么建议么? 彼得,先谢谢你