2 回答
TA贡献1821条经验 获得超6个赞
在这种情况下,您应该编写一个测试。您不需要再写了,因为它只是重复的。此测试应控制字段分配:
@Test
public void propertiesAreSetOnBankAccountConstructor() {
float balance = 100F;
char[] userNameArray = {'u'};
char[] passArray = {'p'};
BankAccount testedObject = new BankAccount(balance, userNameArray, passArray);
assertEquals(balance, testedObject.getBalance(), 0F);
assertSame(userNameArray, testedObject.getUsername());
assertSame(passArray, testedObject.getPassword());
}
更新:如果没有可以使用的吸气剂org.springframework.test.util.ReflectionTestUtils(或者只是纯反射):
@Test
public void propertiesAreSetOnBankAccountConstructor() {
float balance = 100F;
char[] userNameArray = {'u'};
char[] passArray = {'p'};
BankAccount testedObject = new BankAccount(balance, userNameArray, passArray);
assertEquals(balance, ((Float)ReflectionTestUtils.getField(testedObject, "balance")), 0F);
assertSame(userNameArray, ReflectionTestUtils.getField(testedObject, "username"));
assertSame(passArray, ReflectionTestUtils.getField(testedObject, "password"));
}
TA贡献1801条经验 获得超8个赞
请参考这个例子。
import static org.junit.Assert.assertThat;
import org.junit.Test;
public class CalculatorTest {
@Test
public void constructorTest(){
private Calculator c;
c = new Calculator(4, 5);
assertThat(9, is(c.sum()));
}
}
添加回答
举报