我有一堂课:
public class MyEncryptor {
private StringEncryptor stringEncryptor; // this is an interface ref
public void setStringEncryptor(StringEncryptor stringEncryptorImpl) {
if(condition){
this.stringEncryptor = stringEncryptorImpl;
}
}
}
在JUnit中测试方法setStri
我有一个取有理数倒数的代码。我也有一个构造函数。这就是我所拥有的:
private int num;
private int den;
public Rational(int numIn, int denIn) {
num = numIn;
den = denIn;
}
public String reciprocal() {
return den + "/" + num;
}
我想为这段代码编写一个JUnit测试。到目前为止,这就是我所拥有的,但它一直在失败:
int num = 7;
int den = 5;
@Test
public void re
下面的Junit测试用例文件
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestJunit {
@Test
public void testAdd() {
String str= "Junit is working fine";
assertEquals("Junit is working fine",str);
}
}
=============================
我在Java1.7上使用JMockit 1.15。
我想验证一下,一些经过测试的代码首先创建了一个特定类(ExampleClass)的新实例,然后在这个实例上调用了一个方法。我尝试了多种方法,使用验证、期望、@、@Injectable等多种不同的方式,但我无法成功。
下面是一个例子。
ExampleClass是一个类;我将对它进行模拟,并希望验证它是如何使用的。
public class ExampleClass {
final double id = Math.random();
public ExampleClass() {
}
public void
我在springboot中有这个验证器,当一个整数不是介于1到3之间时,它会产生错误,并且我使用addConstraintViolation从属性文件打印消息。
public class SizeValidator implements ConstraintValidator<SizeValidation, Integer> {
private int maxSize = 500;
private int minSize = 1;
@Override
public void initialize(SizeValidation constraintA
我有一个在线程中运行的进程(用作实时信号分析进程)。我想为线程进程提供一个已知的输入,然后用jUnit测试输出是否正确。我有一个回调侦听器,它可以在线程完成处理数据时通知我,并且我可以通过将测试用例本身注册为侦听器来成功地对结果运行断言。
When those assertions fail, they do throw an exception. But that exception is not registered as a failure by jUnit, presumably because they are happening outside of a test method.
我对单元测试相当陌生,并且在网上阅读了很多材料,但我在找出一些东西时遇到了一些小麻烦。
例如:如果我有以下代码:
public class RandomClass
{
int a = 4;
public void foo(Object obj)
{
if(a == 4)
{
AnotherObject anotherObj = new AnotherObject();
anotherObj.setAttribute(obj.getFiled());
//Method call that will be suppressed
我不确定我是否以正确的方式测试一个void返回方法,也不确定我的class-under-test (剪切)是否需要任何更改才能使它100%可测试和防bug。
我在执行NullPointerException时看到了loginOperations,因为loginOperations没有设置。
错误:
java.lang.NullPointerException
at com.demo.service.LoginService.doLogin(LoginService.java:40)
at com.demo.service.LoginServiceTest.doLogin(Log
下面是我的JUnit5测试类的一个片段。
@Test
@DisplayName("This is just a simple JUnit 5 test")
void testSimple() {
assertEquals(2, 3, "this is the 1st assertEquals");
assertEquals(4, 5, "this is the 2nd assertEquals");
assertEquals(5, 6, "this is the 3rd asswerEquals");
}
我的类中有几个方法
public class StringChecking
{
public static void main(String[] args);
public void stringChecker(String text, int number); //takes a string and prints that out.
}
我想编写单元测试来测试“stringChecker()”方法。我想知道我该怎么做。当我在JUnit测试类中创建StringChecking类型的对象时,似乎无法从该实例访问stringChecker()方法。
根据参数的不同,StringChecker方
我有以下测试:
@Test
public void testValues() {
User user = new User();
// user.getCode().equals("CODE");
assert user.getCode().equals("CODE");
}
此测试的预期行为将失败,因为getCode尚未初始化,因此计算结果为null。我希望会抛出一个NullPointerException。但这并没有发生,而是测试通过了。
取消对行的注释,测试将按照预期的方式在NullPointerException中失败。
那么,我的想
我现在遇到的问题是,我的配置方法抛出了一个异常,说明配置错误,但我不知道问题是什么。
下面是我的配置方法:
public class ModelMapperEmb {
public CasinoDto modelToDto(Casino casino){
ModelMapper modelMapper = new ModelMapper();
modelMapper.addMappings(new PropertyMap<Casino, CasinoDto>() {
@Override
pro
我是jUnit测试新手,下面的代码需要进行测试。我正在尝试测试add(Object )和Object at( index)。我该怎么做呢?
public class ResizingArray
{
// declare the one stack.
private Object[] Stack;
private int length; //array length
private int n; //number of elements
public ResizingArray(int n) //Constructor
{
我遇到了一些奇怪的事情。功能示例:
public class F {
public enum Pawn {Black, White}
public Pawn x;
public F(){
this.x = Pawn.Black;
}
public Pawn test(){
return this.x;
}
}
为此进行单元测试:
import junit.framework.Assert;
import junit.framework.TestCase;
import <package ... >.F;
public class
assertArrayEquals不适用于用户定义的类。 我有一个只有name属性的User类。当我为User类创建数组对象(actual和expected)并尝试使用assertArrayEquals进行测试时,测试失败。 我得到了以下错误 arrays first differed at element [0]; expected:<cts.Learning.User@3fa77460> but was:<cts.Learning.User@619a5dff>" 我使用的是Junit - 4.12和Hamcrest-all 1.3版本。 public cl
我正在尝试为视图模型创建一些测试。
视图模型包括一个调用Database的Database实例化
CallRoomDatabase db = CallRoomDatabase.getDatabase(application);
其中getDatabase采用Dao()的实例
@Database(entities = {CallEntity.class}, version = 1)
public abstract class CallRoomDatabase extends RoomDatabase {
public abstract CDao cDao();
// marking t
当我有以下方法时,junit测试会是什么:
@Override
public void saveLastSuccesfullLogin(final User user) {
gebruiker.setLastLogin(new Date());
storeUser(user);
}
子方法storeUser:
@Override
public void storeUser(final User user) {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
我有以下断言语句;
assertTrue(xyz.getTotalRecords() == 50);
我收到一个错误
java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:92)
at org.junit.Assert.assertTrue(Assert.java:43)
at org.junit.Assert.assertTrue(Assert.java:54)
是我做错了什么,还是与实际断言的内容有问题?
希望了解如何在向控制器传递新对象时编写JUnit测试用例。
下面是Spring控制器中传递新PriceIncrease对象的代码。
@RequestMapping(method=RequestMethod.GET, value="/priceincrease")
public String showPriceIncreasePage(Map<String, Object> map){
map.put("priceIncrease", new PriceIncrease());
logger.info("Returning P
我正在学习JUnit,我正在测试一个简单的方法,我有一个方法来填充一个列表并返回它。虽然在单元测试中我提供了相同的列表,但是单元测试失败了。
码
public List<Transaction> retrieveData() throws ParseException {
List<Transaction> expResult = new ArrayList();
Date convertedDate = new SimpleDateFormat("yyyy-MM-dd").parse("2014-04-29T13
嗨,伙计们,我的第一次考试有点问题
我写了这个片段,但我一直有这样的问题:
org.junit.Assert.assertNotNull(Assert.java:712) at org.junit.Assert.assertNotNull(Assert.java:722) java.lang.AssertionError at org.junit.Assert.fail(Assert.java:86) at org.junit.Assert.assertTrue(Assert.java:41)
有人知道热帮我吗?也许在这门课上,哪一种测试是正确的呢?非常感谢
我已经在gradle中实现