junit

Java Enumerating list in mockito's thenReturn

爱⌒轻易说出口 提交于 2019-12-21 09:37:03
问题 Is there a way to enumerate the items in a list within mockito's thenReturn function so I return each item in a list. So far I've done this: List<Foo> returns = new ArrayList<Foo>(); //populate returns list Mockito.when( /* some function is called */ ).thenReturn(returns.get(0), returns.get(1), returns.get(2), returns.get(3)); This works exactly how I want it to. Each time the function is called, it returns a different object from the list, e.g get(1) , get(2) etc. But I want to simplify this

Is there a way to reinitialize a static class in Java?

若如初见. 提交于 2019-12-21 09:22:14
问题 I'm trying to unit test a class that references static data from another class. I cannot "not" use this static class, but obviously running multiple tests has become problematic. So my question is this. Is there a way in a junit test to reinitialize a static class? That way one test is not effected by a previous test? So in other words some way of doing this: Foo.setBar("Hello"); // Somehow reinitialize Foo String bar = Foo.getBar(); // Gets default value of bar rather than "Hello"

Spring MockMvc redirectedUrl with pattern

折月煮酒 提交于 2019-12-21 09:07:07
问题 I have a simple PersonController class that provides save() method to persist the object from http post request. package org.rw.controller; import java.sql.Timestamp; import java.util.List; import org.rw.entity.Person; import org.rw.service.PersonService; import org.rw.spring.propertyeditor.TimestampPropertyEditor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org

Run tests from inner classes via Maven

我的未来我决定 提交于 2019-12-21 08:26:09
问题 I have following tests structure: public class WorkerServiceTest { public class RaiseErrorTest extends AbstractDbUnitTest{ @Test public void testSomething(){ } ... } ... } It's done because I don't want to create a separate class file for every test case extending AbstractDbUnitTest . The problem is that mvn test doesn't run test from my inner class. Is there is a way how to configure Maven to run such tests? Of course I can create methods in the parent class calling the inner class methods

Which dependencies do I need to use Mockito and JUnit in an Eclipse RCP Tycho project

旧城冷巷雨未停 提交于 2019-12-21 08:00:11
问题 This is my current test fragment: <packaging>eclipse-test-plugin</packaging> <dependencies> <dependency> <groupId>org.junit</groupId> <artifactId>com.springsource.org.junit</artifactId> <version>4.7.0</version> </dependency> </dependencies> with the following plugins configuration: <plugin> <groupId>org.eclipse.tycho</groupId> <artifactId>tycho-surefire-plugin</artifactId> <version>${tycho.version}</version> <configuration> <dependencies> <dependency> <type>p2-installable-unit</type>

If I @Ignore a test class in JUnit4, does @BeforeClass still run?

橙三吉。 提交于 2019-12-21 07:55:43
问题 Quick background: I've been hunting down a Maven / Surefire test-running problem for days now, and I've narrowed it down to a small number suspect of tests. The behavior I'm seeing is insane . I start with mvn clean test : 250 tests run, 0 skipped. Now, I move the suspect test into src/test/java and try again: 146 tests run, 0 skipped! The output of Maven gives no clue that other tests aren't being run, even with the -X flag. That brings me to my question: the reason I call the test 'suspect'

Write JUnit test for @ExceptionHandler

ε祈祈猫儿з 提交于 2019-12-21 07:55:39
问题 I am writing a Rest service using Spring MVC. Here is the outline of the class: @Controller public class MyController{ @RequestMapping(..) public void myMethod(...) throws NotAuthorizedException{...} @ExceptionHandler(NotAuthorizedException.class) @ResponseStatus(value=HttpStatus.UNAUTHORIZED, reason="blah") public void handler(...){...} } I have written my unit tests using the design posted here. The test is basically as follows: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(

Hamcrest - what version to use? 1.3 or 2

隐身守侯 提交于 2019-12-21 06:47:10
问题 I am quite confused. Currently I am testing my spring application using <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> I was happy as long as I wanted to match RegularExpressions. In hamcrest 1.3 you need to write your own matcher, which I did not like that much. I searched and found that hamcrest 2.0 has something build in, like: assertThat(DateHelper.getActualDateForXML(), MatchesPattern

Spring MVC 学习总结(十)——Spring+Spring MVC+MyBatis框架集成(IntelliJ IDEA SSM集成)

青春壹個敷衍的年華 提交于 2019-12-21 06:46:41
Spring MVC 学习总结(十)——Spring+Spring MVC+MyBatis框架集成(IntelliJ IDEA SSM集成) 目录 一、新建一个基于Maven的Web项目 1.5、Idea整合Tomcat 1.6、将依赖包添加到Web-Inf目录下 二、创建数据库与表 三、添加依赖包 四、新建POJO实体层 五、新建MyBatis SQL映射层 六、JUnit测试数据访问(非必要) 七、完成Spring整合MyBatis配置 八、配置web.xml加载Spring容器与MVC 九、创建服务层 十、完成图书管理功能 10.1、图书列表 11.2、删除与多删除功能 11.3、新增图书功能 11.4、编辑图书功能 11.5、日志、首页、样式与最终的控制器 十二、总结与问题 十三、示例下载与预览 与SSH(Struts/Spring/Hibernate/)一样,Spring+SpringMVC+MyBatis也有一个简称SSM,Spring实现业务对象管理,Spring MVC负责请求的转发和视图管理, MyBatis作为数据对象持久化引擎。这样搭配的优点是:轻量、自由度高、Spring与Spring MVC契合度更好。通过一个图书管理示例完成SSM框架的集成,可以将前面学习过的一些内容整合起来,使用到的知识包含:Spring、Spring MVC、MyBatis

JUNIT test case for connection with database

你。 提交于 2019-12-21 06:15:11
问题 This is the code which im testing for JDBC connection package com.sybase; public class SybaseDBConnection { public static Properties prop = null; public static Connection getConnection(String databaseType) { Connection conn = null; // Properties prop = null; String database = null; String driver = null; String url = null; String user = null; String password = null; try { // prop = new Properties(); // prop.load(SybaseDBConnection.class.getClassLoader() // .getResourceAsStream("com/properties