mirror of
https://github.com/robindhole/fundamentals.git
synced 2025-03-15 22:59:53 +00:00
Inheritance and ctors.
This commit is contained in:
parent
caab717466
commit
d7deb92977
@ -25,6 +25,12 @@
|
|||||||
<version>4.11</version>
|
<version>4.11</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.18.24</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.scaler.lld.scaler;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class Student extends User {
|
||||||
|
|
||||||
|
private String batchName;
|
||||||
|
private Integer psp;
|
||||||
|
private StudentStatus status = StudentStatus.ACTIVE; // ACTIVE, PAUSED, COMPLETED
|
||||||
|
|
||||||
|
// Define parametrised constructor
|
||||||
|
|
||||||
|
public Student(String name, String email, String batchName, Integer psp) {
|
||||||
|
super(name, email);
|
||||||
|
this.batchName = batchName;
|
||||||
|
|
||||||
|
if (psp < 0 || psp > 100) {
|
||||||
|
throw new IllegalArgumentException("PSP should be between 0 and 100");
|
||||||
|
}
|
||||||
|
this.psp = psp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void changeBatch(String batchName) {
|
||||||
|
this.batchName = batchName;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.scaler.lld.scaler;
|
||||||
|
|
||||||
|
public enum StudentStatus {
|
||||||
|
ACTIVE, PAUSED, COMPLETED, EDGE
|
||||||
|
}
|
19
oop/code/oop/src/main/java/com/scaler/lld/scaler/User.java
Normal file
19
oop/code/oop/src/main/java/com/scaler/lld/scaler/User.java
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package com.scaler.lld.scaler;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class User {
|
||||||
|
private String name;
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
public void changeEmail(String email) {
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
package com.scaler.lld.scaler;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class StudentTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDefaultCtor() {
|
||||||
|
Student student = new Student();
|
||||||
|
|
||||||
|
assertNotNull("If the ctor is called, then object must be returned",
|
||||||
|
student);
|
||||||
|
assertNull("If default ctor is used, name should be null", student.getName());
|
||||||
|
assertEquals("If default ctor is used, status should be active", StudentStatus.ACTIVE, student.getStatus());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParametrisedCtor() {
|
||||||
|
Student student = new Student("John Doe", "john@doe.in", "Batch 1", 100);
|
||||||
|
|
||||||
|
assertNotNull("If the ctor is called, then object must be returned",
|
||||||
|
student);
|
||||||
|
assertEquals("If name ctor is passed to ctor, name should be set in instance", "John Doe", student.getName());
|
||||||
|
assertEquals("If status is not passed to ctor, status should be active", StudentStatus.ACTIVE,
|
||||||
|
student.getStatus());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void testParametrisedCtorWithInvalidPsp() {
|
||||||
|
new Student("John Doe", "john@doe.in", "Batch 1", 101);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParentFields() {
|
||||||
|
Student student = new Student("John Doe", "john@doe.in", "Batch 1", 100);
|
||||||
|
assertEquals("If name ctor is passed to ctor, name should be set in instance", "John Doe", student.getName());
|
||||||
|
|
||||||
|
String newEmail = "john@doe.com";
|
||||||
|
student.changeEmail(newEmail);
|
||||||
|
assertEquals("If email is changed, then email should be updated", newEmail, student.getEmail());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Coverage - % of code your test cases cover - 80%
|
||||||
|
// pytest > junit
|
Loading…
x
Reference in New Issue
Block a user