mockito

mockito: Is there a way of capturing the return value of stubbed method?

回眸只為那壹抹淺笑 提交于 2020-02-01 12:57:01
问题 If I mock a method to return a new instance of some object, how can I capture the returned instance? E.g.: when(mock.someMethod(anyString())).thenAnswer(new Answer() { Object answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); Object mock = invocation.getMock(); return new Foo(args[0]) } }); Obviously, I can have a field of type Foo and inside answer set it to the new instance, but is there a nicer way? Something like ArgumentCaptor? 回答1: Looks like you want to

Spring Best practice for Dependency Injection

我的未来我决定 提交于 2020-01-30 08:39:49
问题 I have some confusion about DI in spring public interface A{ void methodA(); } public class AImpl implements A{ public void methodA(){ // todo something } } public interface B{ void methodB(); } public class BImpl implements B{ public void methodB(){ //todo somethong } } I have two interface A and B and 2 class implements it. So, I have 2 Class that implement interface C, it depend on Interface A and B This is case 1: public interface C{ void methodC(); } public class CAutowired implements C{

Robolectric buildActivity() with Mockito spy?

百般思念 提交于 2020-01-28 10:31:05
问题 It seems to me that building an Activity unit test with Robolectric's lifecycle utilities (starting with Robolectric.buildActivity() ) and spying on the same Activity with a Mockito spy are mutually exclusive. Because buildActivity() controls the construction of the Activity object, the only place to add a spy for the Activity is after calling buildActivity() . However, the spy doesn't function properly when it's added after the fact. This is especially true when spying for side effects of

在spring中简单使用Mockito解决Bean依赖树问题

与世无争的帅哥 提交于 2020-01-28 00:48:37
前提 本文 不是针对Mockito的入门教学 ,主要叙述如何简单的使用Mockito解决Bean依赖树问题,对于Mockito的学习请找其他的文章或者查阅官方文档 基本概念 Junit初始化及存在的问题 spring应用在unit test时,test是 独立运行 的,所以需要自行 init ApplicationContext,启动 Ioc容器。 Junit要求: Test类中涉及的所有Spring bean 注入成功才能完成applicationContext初始化,并启动IOC容器,否则无法执行unit test。 ApplicationContext初始化的两种方式 手动注入(使用 @Bean或者 @Component 注入所需的类) 编写@Configuration 类(使用@ComponentScan 指定扫描beans) 两种初始化方式存在的问题 方式一: 所需的beans中,一个bean少注入了就会导致无法初始化上下文 需要注入的bean太多时,需要花费大量的时间和精力,排查缺漏难度大 方式二: 颗粒度难以把控,随着项目规模变大之后,可能导致bean导入过多,单元测试跑很久才能通过 当项目规模大了之后,bean之间的依赖往往是复杂的,扫描bean的方式可能出现一些不属于自己模块的未知问题或者某些中间件在unitTest环境无法正常启动,导致无法初始化上下文

EasyMock deep stubs

故事扮演 提交于 2020-01-24 17:14:28
问题 I have to mock the following security step using EasyMock or UnitilsMock. Could you please suggest a way to achieve this? String id = context.getCallerPrincipal().getName(); This step is related to security. So I will not be able to create a Principle object and make a two tier mocking. I know that mockito handles such stuff easily as follows, @Mock(answer = Answers.RETURNS_DEEP_STUBS) SessionContext mockContext; But, I need a similar solution using EasyMock or Unitils Mock. The complete code

How to write junit test cases for rest controller, service and dao layer using springboot?

£可爱£侵袭症+ 提交于 2020-01-24 13:18:26
问题 How to write JUnit Test cases for RestController , Service and DAO layer ? I've tried MockMvc @RunWith(SpringRunner.class) public class EmployeeControllerTest { private MockMvc mockMvc; private static List<Employee> employeeList; @InjectMocks EmployeeController employeeController; @Mock EmployeeRepository employeeRepository; @Test public void testGetAllEmployees() throws Exception { Mockito.when(employeeRepository.findAll()).thenReturn(employeeList); assertNotNull(employeeController

How to write junit test cases for rest controller, service and dao layer using springboot?

蹲街弑〆低调 提交于 2020-01-24 13:18:08
问题 How to write JUnit Test cases for RestController , Service and DAO layer ? I've tried MockMvc @RunWith(SpringRunner.class) public class EmployeeControllerTest { private MockMvc mockMvc; private static List<Employee> employeeList; @InjectMocks EmployeeController employeeController; @Mock EmployeeRepository employeeRepository; @Test public void testGetAllEmployees() throws Exception { Mockito.when(employeeRepository.findAll()).thenReturn(employeeList); assertNotNull(employeeController

How to Unit-Test Thread which takes time to perform action

感情迁移 提交于 2020-01-24 12:59:25
问题 I have Thread which runs while the program runs and polls a queue and check whether it has object and if yes then it calls method on the object Here is the code : while(isRunning){ synchronized (loginQueue) { if(loginQueue.peek() != null) { Object[] loginObjectWithConnection = loginQueue.poll(); tryLogin(loginObjectWithConnection); } } try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } Here is the tryLogin method private void tryLogin(Object[]

Mockito runnable: wanted but not invoked?

别说谁变了你拦得住时间么 提交于 2020-01-24 10:03:38
问题 I want to make sure I'm not doing something terribly wrong before I submit a bug report. This is really weird. The setup: robolectric 3.0; mockito 1.10.19 Unit under test: public BbScrollView( Context context ){ this( context, null ); } public BbScrollView( Context context, AttributeSet attrs ) { super( context, attrs ); mScrollTask = new Runnable() { public void run() { checkForStopped(); } }; } public void checkForStopped(){ int newPosition = getScrollY(); // the rest is irrelevant , but I

Mocking static private final variable using Powermock?

爷,独闯天下 提交于 2020-01-24 09:44:05
问题 I have a utility class which is a final class. There i have used injection for injecting LOGGER. public final class Utilities { @Inject private static Logger.ALogger LOGGER; private Utilities() { //this is the default constructor. so there is no implementation } public static String convertToURl(string input){ try{ //do some job }catch(IllegalArgumentException ex){ LOGGER.error("Invalid Format", ex); } } } While I writing unit testing for this method i have to mock LOGGER otherwise it will