This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 588
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from microservices-demo/test/coverage
Increase code coverage. Remove unnecessary config. Move files.
- Loading branch information
Showing
8 changed files
with
182 additions
and
65 deletions.
There are no files selected for viewing
45 changes: 0 additions & 45 deletions
45
src/main/java/works/weave/socks/ShippingServiceApplication.java
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
src/main/java/works/weave/socks/shipping/ShippingServiceApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package works.weave.socks.shipping; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class ShippingServiceApplication { | ||
public static void main(String[] args) throws InterruptedException { | ||
SpringApplication.run(ShippingServiceApplication.class, args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...works/weave/socks/ShippingController.java → ...pping/controllers/ShippingController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
src/test/java/works/weave/socks/shipping/controllers/ITShippingController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package works.weave.socks.shipping.controllers; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.amqp.AmqpException; | ||
import org.springframework.amqp.rabbit.core.RabbitTemplate; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import works.weave.socks.shipping.entities.Shipment; | ||
|
||
import static org.hamcrest.CoreMatchers.*; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Matchers.anyString; | ||
import static org.mockito.Mockito.*; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
public class ITShippingController { | ||
@MockBean | ||
private RabbitTemplate rabbitTemplate; | ||
|
||
@Autowired | ||
private ShippingController shippingController; | ||
|
||
@Test | ||
public void getShipment() throws Exception { | ||
String shipping = shippingController.getShipping(); | ||
assertThat(shipping, is(notNullValue())); | ||
} | ||
|
||
@Test | ||
public void getShipmentById() throws Exception { | ||
String shipping = shippingController.getShippingById("id"); | ||
assertThat(shipping, is(notNullValue())); | ||
} | ||
|
||
@Test | ||
public void newShipment() throws Exception { | ||
Shipment original = new Shipment("someName"); | ||
Shipment saved = shippingController.postShipping(original); | ||
verify(rabbitTemplate, times(1)).convertAndSend(anyString(), any(Shipment.class)); | ||
assertThat(original, is(equalTo(saved))); | ||
} | ||
|
||
@Test | ||
public void doNotCrashWhenNoQueue() throws Exception { | ||
doThrow(new AmqpException("test error")).when(rabbitTemplate).convertAndSend(anyString(), any(Shipment.class)); | ||
Shipment original = new Shipment("someName"); | ||
Shipment saved = shippingController.postShipping(original); | ||
verify(rabbitTemplate, times(1)).convertAndSend(anyString(), any(Shipment.class)); | ||
assertThat(original, is(equalTo(saved))); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/test/java/works/weave/socks/shipping/entities/UnitPojo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package works.weave.socks.shipping.entities; | ||
|
||
import com.openpojo.reflection.PojoClass; | ||
import com.openpojo.reflection.PojoClassFilter; | ||
import com.openpojo.reflection.filters.FilterClassName; | ||
import com.openpojo.reflection.impl.PojoClassFactory; | ||
import com.openpojo.validation.Validator; | ||
import com.openpojo.validation.ValidatorBuilder; | ||
import com.openpojo.validation.affirm.Affirm; | ||
import com.openpojo.validation.rule.impl.*; | ||
import com.openpojo.validation.test.impl.GetterTester; | ||
import com.openpojo.validation.test.impl.SetterTester; | ||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.hamcrest.CoreMatchers.*; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class UnitPojo { | ||
// Configured for expectation, so we know when a class gets added or removed. | ||
private static final int EXPECTED_CLASS_COUNT = 1; | ||
|
||
// The package to test | ||
private static final String POJO_PACKAGE = "works.weave.socks.shipping.entities"; | ||
|
||
private final PojoClassFilter filter = new FilterClassName("^((?!Unit).)*$"); | ||
|
||
@Test | ||
public void ensureExpectedPojoCount() { | ||
List<PojoClass> pojoClasses = PojoClassFactory.getPojoClasses(POJO_PACKAGE, filter); | ||
Affirm.affirmEquals("Classes added / removed?", EXPECTED_CLASS_COUNT, pojoClasses.size()); | ||
} | ||
|
||
@Test | ||
public void testPojoStructureAndBehavior() { | ||
Validator validator = ValidatorBuilder.create() | ||
// Add Rules to validate structure for POJO_PACKAGE | ||
// See com.openpojo.validation.rule.impl for more ... | ||
.with(new GetterMustExistRule()) | ||
.with(new SetterMustExistRule()) | ||
// Add Testers to validate behaviour for POJO_PACKAGE | ||
// See com.openpojo.validation.test.impl for more ... | ||
.with(new SetterTester()) | ||
.with(new GetterTester()) | ||
// Static fields must be final | ||
.with(new NoStaticExceptFinalRule()) | ||
// Don't shadow parent's field names. | ||
.with(new NoFieldShadowingRule()) | ||
// What about public fields, use one of the following rules | ||
// allow them only if they are static and final. | ||
.with(new NoPublicFieldsExceptStaticFinalRule()) | ||
.build(); | ||
|
||
validator.validate(POJO_PACKAGE, filter); | ||
} | ||
|
||
@Test | ||
public void testEquals() throws Exception { | ||
assertThat(new Shipment("id", "name"), is(equalTo(new Shipment("id", "name")))); | ||
assertThat(new Shipment("id", "name"), is(equalTo(new Shipment("id", "another")))); | ||
assertThat(new Shipment("id", "name"), is(not(equalTo(new Shipment("another", "name"))))); | ||
} | ||
|
||
@Test | ||
public void testHashcode() throws Exception { | ||
assertThat(new Shipment("id", "name").hashCode(), is(equalTo(new Shipment("id", "name").hashCode()))); | ||
assertThat(new Shipment("id", "name").hashCode(), is(equalTo(new Shipment("id", "another").hashCode()))); | ||
assertThat(new Shipment("id", "name").hashCode(), is(not(equalTo(new Shipment("aa", "name").hashCode())))); | ||
} | ||
|
||
@Test | ||
public void testString() throws Exception { | ||
assertThat(new Shipment("id").toString(), is(notNullValue())); | ||
|
||
} | ||
} |