Tag: junit

Mockito – 存根抽象父类方法

我看到非常奇怪的行为试图存根一个在抽象父类MyAbstractBaseClass定义的类MyClass的方法myMethod(param) 。 当我尝试存根(使用doReturn(“…”).when(MyClassMock).myMethod(…)等)此方法失败时,在不同的场景下抛出不同的exception。 在该行上抛出exception。 当我使用doReturn(“…”).when(MyClassMock).myMethod(CONCRETE PARAM CLASS OBJECT) ,我得到以下exception: org.mockito.exceptions.misusing.WrongTypeOfReturnValue: String cannot be returned by hasValidExpirationDate() hasValidExpirationDate() should return boolean at … hasValidExpirationDate()不是一个被存根的方法,但它是由抽象基类中MyMethod(param)的真实实现MyMethod(param) 。 当我使用doReturn(“…”).when(MyClassMock).myMethod(any(PARAMCLASS.class)) ,我得到以下exception: org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 0 matchers expected, 1 recorded. This exception may occur if matchers are combined with raw values: 等 但是当我在子类MyClass定义方法myMethod(param) ,代码不再失败。 我在MyClass具体实现只是调用super.myMethod(param)并返回它,因此除了修复unit testing之外它没有任何影响。 所以看起来Mockito只能在类中定义的存根方法被模拟自己,而不是超类。 我正在阅读Mockito文档,我不知道它所说的inheritance方法无法被删除。 […]

可以选择将unit testing或集成测试添加到现有系统中,哪个更好开始,为什么?

我目前正在咨询现有系统,我怀疑正确的下一步是添加unit testing,因为正在发生的exception类型(空指针,空列表,无效的返回数据)。 但是,在应用程序中进行“个人投资”的员工坚持进行集成测试,即使报告的问题与特定用例失败无关。 在这种情况下,最好从unit testing还是集成测试开始?

为本地@ExceptionHandler编写JUnit测试

我有以下控制器: class Controller { @ResponseStatus(HttpStatus.OK) @RequestMapping(value = “/verifyCert”, method = RequestMethod.GET) public void verifyCertificate() throws CertificateExpiredException, CertificateNotYetValidException { certificate.checkValidity(); } @ResponseStatus(HttpStatus.FORBIDDEN) @ExceptionHandler({CertificateExpiredException.class, CertificateNotYetValidException.class}) public void handleCertificateValidityException(Exception e) {} } 我的目标是,如果证书无效,该控制器将重定向到exception处理程序。

模拟对象创建内部方法测试中

我有一个我想测试的类。只要有可能,我会依赖于其他类的对象对该类进行dependency injection。但是,我遇到了一个案例,我想在没有重构代码的情况下模拟对象而不是申请DI。 这是被测试的课程: public class Dealer { public int show(CarListClass car){ Print print=new Print(); List list=new LinkedList(); list=car.getList(); System.out.println(“Size of car list :”+list.size()); int printedLines=car.printDelegate(print); System.out.println(“Num of lines printed”+printedLines); return num; } } 我的测试类是: public class Tester { Dealer dealer; CarListClass car=mock(CarListClass.class); List carTest; Print print=mock(Print.class); @Before public void setUp() throws Exception { dealer=new Dealer(); […]

在运行JUnit时,使用CDI bean注入EJB 3.1的问题

我使用@inject创建了一个EJB3.1并注入了CDI bean,但是在unit testing时遇到了一些问题,但是当从servlet测试它工作正常时。 我在WEB-INF文件夹中有beans.xml。 下面是我的EJB代码: @Stateless public class CdiUsingEjb { @Inject private HelloServletCDIPojo helloServletCDIPojo; public String greet() { assert helloServletCDIPojo != null; return helloServletCDIPojo.from(); } } 下面是我的CDI bean: public class HelloServletCDIPojo { public String from() { return “from HelloServletStateless CDI”; } } 我创建了一个JUnit类: public class CdiUsingEjbTest { @EJB private CdiUsingEjb cdiUsingEjb; @Before public void setUp() […]

如何为JUnit测试设置Loglevel

我在我的课程中使用java日志。 例: public class MyClass { private static Logger logger = Logger.getLogger(MyClass.class.getName()); …. } 当我为该课程编写JUnit测试时,我想将Loglevel设置为’FINE’。 我试过了: @Before public void setup() throws PluginException { Logger.getGlobal().setLevel(Level.FINE); …. } 但这没有效果。 使用Java Logging时,如何在JUnit Test中控制loglevel? 我正在使用Maven运行我的测试。

如何使用JUnit + Mockito模拟您尝试进行unit testing的服务中使用的类

我想为使用/依赖于另一个类的服务编写unit testing。 我想做的是模拟依赖类的行为(而不是该类的实例)。 正在测试的服务方法在内部使用依赖类(即,依赖类的实例未传入方法调用)例如,我有一个我想要测试的服务方法: import DependentClass; public class Service { public void method() { DependentClass object = new DependentClass(); object.someMethod(); } } 在我对Service method()的unit testing中,我想在DependentClass实例上模拟someMethod()而不是让它使用真实的。 我如何在unit testing中进行设置? 我见过的所有示例和教程都显示了传递给正在测试的方法的模拟对象实例 ,但我没有看到任何显示如何模拟类而不是对象实例的内容 。 这可能与Mockito(当然是)?

使用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表达式,知道如何解决我的问题?

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”); […]