我知道这个问题被问了很多次,但我遵循了很多答案,但仍然不起作用
在此链接中,他们说我们必须在 gradle 中添加:
testImplementation 'org.mockito:mockito-inline:2.13.0'
Run Code Online (Sandbox Code Playgroud)
=>目前我有
testImplementation "org.mockito:mockito-inline:2.28.2"
Run Code Online (Sandbox Code Playgroud)
我的 MockMaker 文件中也有这一行:
mock-maker-inline
Run Code Online (Sandbox Code Playgroud)
然后你可以看到我的以下代码:
object ApiHelper {
fun <T> createService(
url: String,
clazz: Class<T>
): T
}
Run Code Online (Sandbox Code Playgroud)
在我的 UITEST 中
@Mock
private lateinit var service: myService
private lateinit var apiHelper: ApiHelper
@Before
fun setUp() {
apiHelper = mock(ApiHelper::class.java)
given(ApiHelper.createService(
anyString(),
MyService::class.java,
)).willReturn(service)
}
Run Code Online (Sandbox Code Playgroud)
我的代码有什么问题吗?我错过了什么吗?
我有一个设置背景颜色的可组合项,我想测试一下。
@Composable
fun MyComposableButton(
enabledColor: Color,
disableColor: Color,
isEnabled: Boolean = true,
) {
val buttonBackgroundColor = if (enabled) enabledColor else disableColor
Button(
...
enabled = enabled,
colors = ButtonDefaults.textButtonColors(
backgroundColor = buttonBackgroundColor
)
) { ... }
}
Run Code Online (Sandbox Code Playgroud)
我期待编写如下测试:verifyEnabledBackgroundColor和verifyDisabledBakcgroundColor。
我在组合测试中找不到直接可用的任何断言,当尝试创建自己的断言时,我发现使用SemanticMatcther了 a SemanticNode,但构造函数是最新的内部断言,因此无法进行。
我尝试过mock,Color但我做不到,根据这个答案,需要高 API 级别,这对我的项目来说是不行的。
如何测试设置可组合项的背景颜色?
所以我想创建一个Mono<Void>(或任何Mono<SomeIgnorableType>)实际发出元素的。为什么?因为我实际上想运行一个效果,而结果只是意味着该效果已运行,但是,它可以被忽略。
在 Haskell 中,Void 是一种不适宜居住的类型……就像NothingScala 中的一样。在 Java 中,Void 也是不适宜居住的,但它的使用方式更像是UnitScala 中的类型或()Haskell 中的 (0 arity tuple)。一种只有一个居民的类型。
好吧,理论太多了。到目前为止我的选择:
Mono.just((Void) null)抛出异常。
Mono.empty().single()(因为我实际上想发出一个事件),当有人订阅它时,最终会失败。和...
Mono.just("something").then()运行效果,但我无法将它与其他单声道链接,因为它只发出完整和错误事件。
又怎样???
有一个abstract班
public abstract class BaseProcessor {
public BooksTransaction getBooksTransaction() {
return booksTransaction;
}
}
Run Code Online (Sandbox Code Playgroud)
还有一个final class要使用Junit进行测试
public final class CreateOrganisationProcessor extends BaseProcessor {
public boolean process() throws Exception { //method to be tested
request = new CreateOrganisationRequest(IntegrationSystems.valueOf(getBooksTransaction().getSource()),
IntegrationSystems.valueOf(getBooksTransaction().getDestination()), getBooksTransaction());
request.setRequestTypes(getRequestTypes());
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试监视类BaseProcessor并模拟 getBooksTransaction对象的方法return BooksTransaction。
代码:
@Test
public void testProcess() throws Exception {
BaseProcessor spy = Mockito.spy(new CreateOrganisationProcessor());
BooksTransaction booksTransaction = new BooksTransaction();
booksTransaction.setReferenceID(DEFAULT_REFERENCE_ID);
Mockito.doReturn(booksTransaction).when(spy).getBooksTransaction(); …Run Code Online (Sandbox Code Playgroud) 我是 Java 新手,一般没有做过很多单元测试。
有人能告诉我为什么final不能模拟类吗?
我需要模拟java.lang.reflect.Method进行测试(我有动态的东西)。我们已经使用Junit spring&mockito编写了所有测试用例。但是我在为我的方面编写Junit时遇到问题。此外,我们还编写了自定义注释,请对此提供帮助
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/services-test.xml")
@ImportAutoConfiguration(RefreshAutoConfiguration.class)
public abstract class AbsServicesTest extends AbstractJUnit4SpringContextTests {
static{
System.setProperty("LOCAL");
}
}
public class TaxValidatorAspectTest extends AbsServicesTest {
@Autowired
private ResourcesUtils resourcesUtil;
@Mock
private JoinPoint joinPoint;
@Mock
private MethodSignature methodSignature;
@Mock
private Method method;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(new TaxValidatorAspectTest());
Mockito.when(joinPoint.getSignature()).thenReturn(methodSignature);
Mockito.when(methodSignature.getMethod()).thenReturn(method);
}
@Test
public void testBeforeMethod() throws IOException{
TaxValidatorAspect taxonomyValidationAspect = new TaxValidatorAspect();
final Object[] arguments = new Object[4];
arguments[0] = requiredParam1;
arguments[1] = requiredParam2;
arguments[2] = requiredParam3;
Mockito.when(joinPoint.getArgs()).thenReturn(arguments);
taxonomyValidationAspect.before(joinPoint);
}
} …Run Code Online (Sandbox Code Playgroud) 我有一个最终类,它有一个我想执行特定操作的方法。因此我想创建最终类的对象。但我无法创建它,以下是我的课程。
public final class A {
private String name;
A(String name){
this.name = name;
}
public String getName(){
return name;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的 junit 测试用例中,我想创建该类的对象,如下所示
Class TestA{
@Test
public void testA(){
A a = mock(A.class);
when(a.getName()).then("ABC"); //on this line i am getting exception
}
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用 new 关键字,但不起作用。那么有没有办法创建最终类的模拟对象呢?
在我面临异常之后,
org.mockito.exceptions.base.MockitoException:
Cannot mock/spy class A
Mockito cannot mock/spy following:
- final classes
- anonymous classes
- primitive types
at com.rocket.map.resources.TestA.testA(TestA.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) …Run Code Online (Sandbox Code Playgroud) 我在Kotlin写了一个Android Clean Architecture项目,有3个模块:
这三个模块都有使用junit编写的单元测试.但是对于Kotlin,默认情况下每个班级都是最终的.我很快就遇到了问题:如何使用mockito模拟最后一堂课
Mockito 2现在可以实现
它可以通过mockito扩展机制通过创建/mockito-extensions/org.mockito.plugins.MockMaker包含单行的文件来完成:
mock-maker-inline
Run Code Online (Sandbox Code Playgroud)
此解决方案在数据模块(Android库)和演示模块(Android应用程序)上运行良好,但不适用于我的domaine模块(Java库).
我知道,这个问题已经被问(如何嘲弄与一的Mockito final类,调用final类与静态的Mockito方法Mock对象),但我没有找到我要找的答案.