forked from plusone/plusone-commons
重载 equalsCode 方法;添加单元测试
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package xyz.zhouxy.plusone.commons.base;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
|
||||
class IWithCodeTests {
|
||||
|
||||
private static class WithCodeImpl implements IWithCode<String> {
|
||||
@Nonnull
|
||||
private final String code;
|
||||
|
||||
WithCodeImpl(String code) {
|
||||
AssertTools.checkNotNull(code);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
private WithCodeImpl instance;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
instance = new WithCodeImpl("testCode");
|
||||
}
|
||||
|
||||
@Test
|
||||
void equalsCode_SameCode_ReturnsTrue() {
|
||||
assertTrue(instance.equalsCode("testCode"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void equalsCode_DifferentCode_ReturnsFalse() {
|
||||
assertFalse(instance.equalsCode("wrongCode"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void equalsCode_NullCode_ReturnsFalse() {
|
||||
assertFalse(instance.equalsCode((String) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void equalsCode_NullObject_ReturnsFalse() {
|
||||
assertFalse(instance.equalsCode((String) null));
|
||||
}
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
package xyz.zhouxy.plusone.commons.base;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class IWithIntCodeTests {
|
||||
@Test
|
||||
void test() {
|
||||
IWithIntCode instance1 = new WithIntCodeImpl(10);
|
||||
IWithIntCode instance2 = new WithIntCodeImpl(20);
|
||||
IWithIntCode instance3 = new WithIntCodeImpl(10);
|
||||
|
||||
// Test for equalsCode method
|
||||
assertTrue(instance1.equalsCode(10));
|
||||
assertFalse(instance1.equalsCode(20));
|
||||
assertTrue(instance2.equalsCode(20));
|
||||
assertTrue(instance3.equalsCode(10));
|
||||
|
||||
// Test for distinct instances with same code
|
||||
assertTrue(instance1.equalsCode(instance3.getCode())); // because they have the same code
|
||||
assertFalse(instance1.equalsCode(instance2.getCode())); // because they have different codes
|
||||
}
|
||||
}
|
||||
|
||||
class WithIntCodeImpl implements IWithIntCode {
|
||||
private int code;
|
||||
|
||||
public WithIntCodeImpl(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user