Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circleci project setup #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
9 changes: 9 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: 2.1

orbs:
maven: circleci/[email protected]

workflows:
maven_test:
jobs:
- maven/test
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,19 @@ For questions and help:
* Or post in the Slack Community exclusive to the course.

GitHub Issues will not be addressed.


##
- Very Simple beforeClass/each/test/after/afterall guru.springframework.sfgpetclinig.controllers.IndexControllerTest
- Group Assertions guru.springframework.sfgpetclinig.controllers.Person
- Nested Test Classes: guru.springframework.sfgpetclinic.services.map.OwnerMapServiceTest
- @RepeatedTest(x) : PersonRepeatedTest:...
- ParameterizedTests : OwnerTest
- quite useful

## Tag annotation, to make groups of tests
- Annotate a Class or Method with @Tag.
- In IntelliJ, go to Run -> `Edit Configuration` -> Change `Class` to `Tag` and enter the Tag name. In the run dropdown (top-right) you can select different test groups
- In Maven
- In Gradle

44 changes: 44 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@
<version>${junit-platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.18.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
<dependency><!-- Parameterized Tests -->
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit-platform.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -82,6 +100,8 @@
<argLine>
--illegal-access=permit
</argLine>
<!-- <groups>Models</groups>-->
<!-- <excludedGroups>Controllers</excludedGroups>-->
</configuration>
</plugin>
<plugin>
Expand All @@ -93,8 +113,32 @@
--illegal-access=permit
</argLine>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.8.2</version>
</plugin>
</plugins>
</build>

<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>

</reporting>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public String index(){
}

public String oupsHandler(){
return "notimplemented";
throw new ValueNotFoundException();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package guru.springframework.sfgpetclinic.controllers;

public class ValueNotFoundException extends RuntimeException {

public ValueNotFoundException() {
}

public ValueNotFoundException(String message) {
super(message);
}

public ValueNotFoundException(String message, Throwable cause) {
super(message, cause);
}

public ValueNotFoundException(Throwable cause) {
super(cause);
}

public ValueNotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package guru.springframework.sfgpetclinic.model;

public enum OwnerType {
INDIVIDUAL, COMPANY
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public Vet findById(Long id) {
@Override
public Vet save(Vet object) {

if (object.getSpecialities().size() > 0){
if (object.getSpecialities() != null && object.getSpecialities().size() > 0) {
object.getSpecialities().forEach(speciality -> {
if(speciality.getId() == null){
if (speciality.getId() == null) {
Speciality savedSpecialty = specialtyService.save(speciality);
speciality.setId(savedSpecialty.getId());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package guru.springframework.sfgpetclinic;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.TestInstance;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)// necessary for beforeAll, not for beforeEach
@Tag("Controllers")
public interface ControllerTests {

@BeforeAll
default void beforeAll(){
System.out.println("(0) Let's do something in the controller Interface");
}
@BeforeEach
default void beforeEach(){
System.out.println("(0.1) Let's do something in the controller Interface");
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package guru.springframework.sfgpetclinic;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;

import java.util.stream.Stream;

public class CustomArgsProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) throws Exception {
return Stream.of(
Arguments.of("FL", 12, 21),
Arguments.of("OH", 22, 23),
Arguments.of("MI", 42, 26));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package guru.springframework.sfgpetclinic;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.RepetitionInfo;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.TestInfo;

@Tag("RepeatedTests")
public interface ModelRepeatedTests {

@BeforeEach
default void beforeEachConsoleOutputter(TestInfo testInfo, RepetitionInfo repetitionInfo){
System.out.println("Running Test - "+testInfo.getDisplayName() + " - " + repetitionInfo.getTotalRepetitions());
}
}
15 changes: 15 additions & 0 deletions src/test/java/guru/springframework/sfgpetclinic/ModelTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package guru.springframework.sfgpetclinic;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.TestInfo;

@Tag("Models")
public interface ModelTests {


@BeforeEach
default void beforeEachConsoleOutputter(TestInfo testInfo){
System.out.println("Running Test - "+testInfo.getDisplayName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package guru.springframework.sfgpetclinic.controllers;

import guru.springframework.sfgpetclinic.ControllerTests;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.condition.*;

import java.time.Duration;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

//@Tag("Controllers") <- now gets the tag from interface
class IndexControllerTest implements ControllerTests {


IndexController indexController;

@BeforeEach
void setUp() {
indexController = new IndexController();
}

@Test
@DisplayName("Test proper View name is return for index page")
void index() {
assertEquals("index", this.indexController.index(), "Wrong view returned");
assertThat(this.indexController.index()).isEqualTo("index");
}

@Test
@DisplayName("Test exception")
void oupsHandler() {
assertThrows(ValueNotFoundException.class, () -> {
this.indexController.oupsHandler();
});
}

@Disabled("Demo of Timeout")
@Test
void testTimeOut() {
assertTimeout(Duration.ofMillis(100), () -> {
// runs in a single thread
Thread.sleep(2000);
System.out.println("I Got Here");
});
}

@Disabled("Demo of Preemptive Timeout")
@Test
void testTimeOutPreempt() {
assertTimeoutPreemptively(Duration.ofMillis(100), () -> {
// runs in a separate thread
Thread.sleep(2000);
System.out.println("I sure not get here");
});
}

@Disabled("Display Purposes only")
@Test
void testAssumptionTrue() {
// Assume doesn't have to be true; if assumption is false, this is not make the test fail
assumeTrue("GURU".equalsIgnoreCase(System.getenv("VARIABLE")));
}

@Test
void testAssumptionTrueAssumptionIsTrue() {
assumeTrue("GURU".equalsIgnoreCase("GURU"));
}

@EnabledOnOs(OS.MAC)
@Test
public void runOnMac() {
}

@EnabledOnOs(OS.WINDOWS)
@Test
public void runOnWindows() {
}

@EnabledOnJre(JRE.JAVA_8)
@Test
public void runOnJava8() {
}

@EnabledOnJre(JRE.JAVA_9)
@Test
public void runOnJava9() {
}

@EnabledOnJre(JRE.JAVA_10)
@Test
public void runOnJava10() {
}

@EnabledOnJre(JRE.JAVA_11)
@Test
public void runOnJava11() {
}

@EnabledIfEnvironmentVariable(named = "HOME",matches = "ERR")
@Test
void testIfHomeIsErr(){}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package guru.springframework.sfgpetclinic.controllers;

import guru.springframework.sfgpetclinic.ControllerTests;
import guru.springframework.sfgpetclinic.fauxspring.Model;
import guru.springframework.sfgpetclinic.fauxspring.ModelMap;
import guru.springframework.sfgpetclinic.fauxspring.ModelMapImpl;
import guru.springframework.sfgpetclinic.model.Vet;
import guru.springframework.sfgpetclinic.services.SpecialtyService;
import guru.springframework.sfgpetclinic.services.VetService;
import guru.springframework.sfgpetclinic.services.map.SpecialityMapService;
import guru.springframework.sfgpetclinic.services.map.VetMapService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

//@Tag("Controllers") <- now gets the tag from interface
class VetControllerTest implements ControllerTests {

VetService vetService;
SpecialtyService specialtyService;

VetController vetController;

@BeforeEach
void setUp() {
specialtyService = new SpecialityMapService();
vetService = new VetMapService(specialtyService);
vetController = new VetController(vetService);

Vet vet1 = new Vet(1L, "Joe", "Buck", null);
Vet vet2 = new Vet(2L, "Jimmy", "Smith", null);

vetService.save(vet1);
vetService.save(vet2);
}

@Test
void listVets() {
Model model = new ModelMapImpl();
String view = vetController.listVets(model);

assertThat("vets/index").isEqualTo(view);

Set modelAtt = (Set) ((ModelMapImpl)model).getMap().get("vets");
assertThat(modelAtt.size()).isEqualTo(2);
}
}
Loading