Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CACoding.iml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,26 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="TEST">
<library name="JUnit5.8.1">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
<component name="SonarLintModuleSettings">
<option name="uniqueId" value="ce2f91cc-4cf6-4286-a14e-4c3d4f923b96" />
Expand Down
27 changes: 27 additions & 0 deletions test/entity/CommonUserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package entity;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.LocalDateTime;
import static org.junit.jupiter.api.Assertions.*;

class CommonUserTest {

private CommonUser user;

@BeforeEach
void init() {
user = new CommonUser("Paul", "password", LocalDateTime.now());
}

@Test
void getNameTest() {
assertEquals("Paul", user.getName());
}

@Test
void getPasswordTest() {
assertEquals("password", user.getPassword());
}

}
28 changes: 28 additions & 0 deletions test/entity/ExceptionDemoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package entity;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class Calculator {
public void divide(int i, int j) {
int result = i / j;
}
}
class ExceptionDemoTest {
@Test
void exceptionTest() {
Calculator calculator = new Calculator();
// Assert that the calculator.divide call throws an exception
Exception exception = assertThrows(
ArithmeticException.class,
// This is an anonymous method that gets called by the assertThrows method
() -> calculator.divide(1, 0)
);
assertEquals("/ by zero", exception.getMessage());
}
}
90 changes: 90 additions & 0 deletions test/use_case/signup/SignupInteractorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package use_case.signup;

import data_access.InMemoryUserDataAccessObject;
import entity.CommonUserFactory;
import entity.User;
import entity.UserFactory;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;

import static org.junit.jupiter.api.Assertions.*;

class SignupInteractorTest {

@Test
void successTest() {
SignupInputData inputData = new SignupInputData("Paul", "password", "password");
SignupUserDataAccessInterface userRepository = new InMemoryUserDataAccessObject();

// This creates a successPresenter that tests whether the test case is as we expect.
SignupOutputBoundary successPresenter = new SignupOutputBoundary() {
@Override
public void prepareSuccessView(SignupOutputData user) {
// 2 things to check: the output data is correct, and the user has been created in the DAO.
assertEquals("Paul", user.getUsername());
assertNotNull(user.getCreationTime()); // any creation time is fine.
assertTrue(userRepository.existsByName("Paul"));
}

@Override
public void prepareFailView(String error) {
fail("Use case failure is unexpected.");
}
};

SignupInputBoundary interactor = new SignupInteractor(userRepository, successPresenter, new CommonUserFactory());
interactor.execute(inputData);
}

@Test
void failurePasswordMismatchTest() {
SignupInputData inputData = new SignupInputData("Paul", "password", "wrong");
SignupUserDataAccessInterface userRepository = new InMemoryUserDataAccessObject();

// This creates a presenter that tests whether the test case is as we expect.
SignupOutputBoundary failurePresenter = new SignupOutputBoundary() {
@Override
public void prepareSuccessView(SignupOutputData user) {
// 2 things to check: the output data is correct, and the user has been created in the DAO.
fail("Use case success is unexpected.");
}

@Override
public void prepareFailView(String error) {
assertEquals("Passwords don't match.", error);
}
};

SignupInputBoundary interactor = new SignupInteractor(userRepository, failurePresenter, new CommonUserFactory());
interactor.execute(inputData);
}

@Test
void failureUserExistsTest() {
SignupInputData inputData = new SignupInputData("Paul", "password", "wrong");
SignupUserDataAccessInterface userRepository = new InMemoryUserDataAccessObject();

// Add Paul to the repo so that when we check later they already exist
UserFactory factory = new CommonUserFactory();
User user = factory.create("Paul", "pwd", LocalDateTime.now());
userRepository.save(user);

// This creates a presenter that tests whether the test case is as we expect.
SignupOutputBoundary failurePresenter = new SignupOutputBoundary() {
@Override
public void prepareSuccessView(SignupOutputData user) {
// 2 things to check: the output data is correct, and the user has been created in the DAO.
fail("Use case success is unexpected.");
}

@Override
public void prepareFailView(String error) {
assertEquals("User already exists.", error);
}
};

SignupInputBoundary interactor = new SignupInteractor(userRepository, failurePresenter, new CommonUserFactory());
interactor.execute(inputData);
}
}