小编asc*_*erk的帖子

使用Mockito 2模拟服务会导致存根错误

我尝试使用Mockito来模拟一个类的行为.这使用了Mockito 1.x. 迁移到JUnit 5和Mockito 2似乎不再起作用了.

@ExtendWith(MockitoExtension.class)
public class MockitoExample {

  static abstract class TestClass {
    public abstract int booleanMethod(boolean arg);
  }

  @Mock
  TestClass testClass;

  @BeforeEach
  public void beforeEach() {
    when(testClass.booleanMethod(eq(true))).thenReturn(1);
    when(testClass.booleanMethod(eq(false))).thenReturn(2);
  }

  @Test
  public void test() {
    assertEquals(1,testClass.booleanMethod(true));
    assertEquals(2,testClass.booleanMethod(false));
  }
}
Run Code Online (Sandbox Code Playgroud)

期望的是,模拟的TestClass显示了在测试方法中测试的行为.

我得到的错误是:

org.mockito.exceptions.misusing.PotentialStubbingProblem: 

  Strict stubbing argument mismatch. Please check:
   - this invocation of 'booleanMethod' method:
      testClass.booleanMethod(false);
      -> at org.oneandone.ejbcdiunit.mockito_example.MockitoExample.beforeEach(MockitoExample.java:30)
   - has following stubbing(s) with different arguments:
      1. testClass.booleanMethod(false);
        -> at org.oneandone.ejbcdiunit.mockito_example.MockitoExample.beforeEach(MockitoExample.java:29)
  Typically, stubbing argument mismatch indicates user mistake when …
Run Code Online (Sandbox Code Playgroud)

java unit-testing mocking mockito junit5

13
推荐指数
3
解决办法
5920
查看次数

二进制将Int转换为Kotlin中的Float

我想处理一个从文件读取为4字节整数的值,就好像它是一个4字节的IEEE Float并将其转换为这样的变量.有没有经验,如何以简单优雅的方式在Kotlin中完成.

type-conversion kotlin

3
推荐指数
1
解决办法
1800
查看次数

在kotlin流处理

  • Kotlin如何处理以下代码?
  • 是否会创建5000000个整数的集合作为临时集合,或者过滤器会立即将其结果提供给forEach哪个,这意味着只会查看20个整数?
  • 如果没有,我怎么能避免中间收集?

码:

class Tests {
    @Test
    fun test() {
        var counter = 0
        (1..10_000_000).filter { it % 2 == 1 }.forEach {
            counter++
            if (counter > 10)
                return
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

collections stream kotlin

2
推荐指数
1
解决办法
72
查看次数

为什么生产者不会在CDI中继承

鉴于以下课程

    private static class ProducedInSubClass {
    }

    private static class ProducedInSuperClass {
    }

    public static class SuperClass {    
        @Produces
        public ProducedInSuperClass producedInSuperClass = new ProducedInSuperClass();
    }

    public static class SubClass extends SuperClass {
        @Produces
        ProducedInSubClass producedInSubClass = new ProducedInSubClass();
    }

   public static class BeanWithSubClass {          
        @Inject
        SubClass subClass;
        @Inject
        ProducedInSuperClass producedInSuperClass;
        @Inject
        ProducedInSubClass producedInSubClass;
   }
Run Code Online (Sandbox Code Playgroud)

ProducedInSuperClass的注入仍然不满意.这与CDI-Spec第4.2章一致,我知道.

为了使这项工作,我需要扩展SubClass

 @Produces
 ProducedInSuperClass producedInSuperClassInSubClass = producedInSuperClass;
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释一下吗?为什么Injects,Annotations Interceptors ...继承但不是生产者?

java inversion-of-control cdi weld

2
推荐指数
2
解决办法
208
查看次数