我对Mockito非常陌生,我遇到了一个无法解决的问题。我想用Mockito测试一种方法。问题是,在这个方法中,有一个创建的对象,而这个对象有一个函数,我想要模拟它。
例如,这里有一个小示例代码,它说明了我的问题:
public class ClassA {
public functionDoingDBStuff() {
//...........
}
}
public class ClassB {
final ClassA classAObj = null;
public functionXYZ() {
classAObj
考虑以下Java类
public class SomeClass {
public void validateSomething(List<String> exceptions) {
//Nothing here
}
}
public class AnotherClass {
public SomeClass someClass;
public void validate() {
List<String> exceptions = new List<>();
someClass.validateSomething(exceptions
我不知道这是否有可能。我需要使用Powermock来模拟在我需要测试的类的构造函数中调用的私有方法。所以我有一个这样的考试课:
@RunWith(PowerMockRunner.class)
@PrepareForTest(XMLTransaction.class)
public class CloseSummaryOrCloseTrailerResponseTest {
public final static String URL="WL_APPSERVER";
private XMLTransaction xmlTransaction;
@Bef
我如何用Mockito模拟我正在测试的类中的其他类?
例如:
MyClass.java
class MyClass {
public boolean performAnything() {
AnythingPerformerClass clazz = new AnythingPerformerClass();
return clazz.doSomething();
}
}
AnythingPerformerClass.java
class AnythingPerformerClass {
public boolean do
我正在使用编写一个简单的单元测试。
我有一个函数要测试:
public void doSomething() {
foo.getStudent(new School());
}
我的测试用例:
@Test
public void testDoSomething() {
Foo mockedFoo = Mockito.mock(Foo.class);
School mockedSchool = Mockito.mock(School.class);
// I want to pass the mocked school as parameter when food.getS
我在测试中使用mockito,因此我希望模拟在它的AtomicIntegerField上执行一个增量,下面是一个简化的代码。
public class MockTest {
private AtomicInteger ai = new AtomicInteger( 0 );
public int getAi(){
return ai.get();
}
public void increment() {
ai.getAndIncrement();
}
public static void main( String[] arg
为什么模拟对象在测试类实例之后出现“锁定”,如果使用注释@Mock来模拟,则在@Produced方法中出现null。
public class MyTest {
@Rule
public MockitoRule rule = MockitoJUnit.rule();
@Rule
public WeldInitiator weld = WeldInitiator.from(MyTest.class).build();
//@Mock <-- this wont work. Will be null in the @Produces methods
我想测试一个用作包装器的类,它为下面的类提供抽象。
这意味着我需要在那个类中创建一些对象,也就是说,我不会通过构造函数或某种方法来传递协作者。
例如,下面是一个包装类:
public class Abstraction {
private ComplexClass complex;
public class Abstraction(some parameters) {
complex = new Complex(parameters);
}
}
为了能够测试这个类,我有一个想法是创建一个返回复杂类的对象的方法,如下所示:
publ
如果我编写测试,如何注入到ViewModel中。我得到了错误UninitializedPropertyAccessException: lateinit property getAll has not been initialized。我想测试从远程API获取数据。我刚开始编写单元测试,所以我想了解如何在这种情况下编写它。
class MainViewModel @Inject constructor(
private val commandProcessor: CommandProcessor,
private val app: Application
) : Android
我是莫奇托测试用例的新手。我想模拟控制器,它有下面列出的非常简单的服务。我的模拟测试类有一个错误,错误的属性传递给mockito,下面是我的错误
@RunWith(MockitoJUnitRunner.class)
public class CreditCardControllerTest {
@Mock
private CreditCardService creditCardService;
@Mock
Model model;
Person p;
@Mock
CreditCard creditCard ;
@InjectMoc
在我的项目中,我需要调用oracle中的存储过程。此过程接受我创建的对象类型的自定义表。在java方面,我创建了已实现的类型处理程序,并将数组类型数据传递给过程。
)从编码的角度来看,一切都很好:)
现在,我需要为我创建的处理程序类编写junit。这个处理程序调用我的util类来获取StructDescriptor、STRUCT对象。下面是实用程序类代码片段。
Class JDBCUtil {
public static STRUCT getJDBCDataObject(final StructDescriptor structDescriptor, final Connection
我必须使用Junit4和Mockito/PowerMockito覆盖whit块和catch块。异常StateException由getHrest方法启动。
public class Fstate{
….
private Date selectedDate;
private Date dateYes;
private Collection<State> sStateList;
private boolean statePlus;
private String stateVisibility;
@EJB
private Muller mull;
….
public void met
我想对一个函数进行单元测试,该函数除其他外调用另一个私有函数,它从磁盘读取XML文件。在单元测试中,我不希望依赖于磁盘上的真正文件,而是希望使用保存在字符串变量中的XML。
我试图同时模拟DocumentBuilder和DocumentBuilderFactory,但没有成功,因为DocumentBuilder仍然希望从磁盘中读取文件,因此在给FileNotFoundException的单元测试中。
使用PowerMockito是我想避免的事情。
下面是我能够重现问题的一个简短的代码片段。
正在测试的类:
public class XMLReader {
private void r
我正在为我的java类编写测试类。我正在使用Junit5和Mockito。
我使用的是Junit5,它与Power Mockito不兼容,所以我只使用Mockito。
我有class Emp,它有如下的findSalary函数,EmpProfileClient是在构造函数中初始化的。
Class Emp {
......
public void findSalary(empId) {
...
TaxReturn taxReturn = new TaxReturn(EmpProfileClient);
int value = taxReturn.apply(
我有一个奇怪的测试代码,它总是绿色的。同时,其中一个测试不应该是绿色的。请看下面的代码。
这是一个我需要测试的类
public class A {
private String param;
public void print(){
System.out.println(this.param);
}
public static void printHello(){
System.out.println("Hello!");
}
}
和自己的测试
import org.mockito.Spy;
impor
我正在编写以下方法的测试用例
class BeanDefinitions {
public Customizer<ReactiveResilience4JCircuitBreakerFactory> apiEnrichmentLayerCircuitBreakerCustomizer(
final HttpClientConfigurationProperties clientsConfig
) {
return CircuitBreakerCustomizers.fromHttpClientConfig(
我有下面的A类实现,使用spring引导。A是对restTemplate进行GET/POST/PUT RestAPI调用的抽象。测试是用Mockito编写的。
Class A {
@Bean
RestTemplate restTemplate;
public class A(RestTemplate restTemplate){
this.restTemplate = restTemplate;
}
public ResponseEntity perform(String endPoint, String requestBody, String auth, HttpMethod