<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: smotastic</title>
    <description>The latest articles on DEV Community by smotastic (@smotastic).</description>
    <link>https://dev.to/smotastic</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F106334%2F484c2748-ab88-4f4b-9d75-5d79547c3aae.jpg</url>
      <title>DEV Community: smotastic</title>
      <link>https://dev.to/smotastic</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/smotastic"/>
    <language>en</language>
    <item>
      <title>GivenWhenThen Pattern</title>
      <dc:creator>smotastic</dc:creator>
      <pubDate>Mon, 28 Sep 2020 19:10:04 +0000</pubDate>
      <link>https://dev.to/smotastic/givenwhenthen-pattern-5bno</link>
      <guid>https://dev.to/smotastic/givenwhenthen-pattern-5bno</guid>
      <description>&lt;p&gt;There are lots of different ways on how to write, organize and structure your tests.&lt;br&gt;
One of these patterns is &lt;em&gt;Given-When-Then&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The term was developed as part of the Behavior-Driven Development process (&lt;a href="https://dannorth.net/introducing-bdd/"&gt;https://dannorth.net/introducing-bdd/&lt;/a&gt;).&lt;br&gt;
In general it describes a pattern on how to write, and describe a desired scenario.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Given&lt;/strong&gt; describes the initial scenario&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When&lt;/strong&gt; describes the event that happens, which will change the scenario&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Then&lt;/strong&gt; describes the expected outcome of the scenario after the event happened&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  Example Scenario
&lt;/h1&gt;

&lt;p&gt;Defining a scenario using this pattern could look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Scenario: A user wants to buy a book from a well known e-commerce platform.

Given the user owns no books from this platform.
When the user orders and receives the ordered book from the platform.
Then the user should have one book in his possession.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Test-Scenarios in Java
&lt;/h1&gt;

&lt;p&gt;When using this pattern in the world of Software-Development-Testing, it is important to agree on conventions made by the team. This ensures the consistency of all written tests. &lt;/p&gt;

&lt;p&gt;Consistency is a big advantage in helping to quickly understand, and also learn how certain usecases are supposed to work.&lt;br&gt;
Imagine a new developer joins the team. Merely by reading the tests he will gain knowledge of how something should, and even might work in the application.&lt;/p&gt;

&lt;p&gt;In general, these conventions should be defined and agreed upon by the team. In the case of the &lt;em&gt;Given-When-Then pattern&lt;/em&gt; I always pay attention to two factors.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Structuring the testmethod&lt;/li&gt;
&lt;li&gt;Naming the testmethod&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Structure of the Testmethod
&lt;/h2&gt;

&lt;p&gt;As the pattern says, there are always three parts in any given scenario (or usecase),&lt;br&gt;
and the testmethod can be structured in exactly these three parts.&lt;/p&gt;

&lt;p&gt;First is always the &lt;strong&gt;&lt;em&gt;given&lt;/em&gt;&lt;/strong&gt; part. The initial values for the scenario. This is basically the setup for any test.&lt;/p&gt;

&lt;p&gt;Second the &lt;strong&gt;&lt;em&gt;when&lt;/em&gt;&lt;/strong&gt; part. This is the execution of the desired method that is to be tested, or as specified before, the event that happens.&lt;/p&gt;

&lt;p&gt;Third the &lt;strong&gt;&lt;em&gt;then&lt;/em&gt;&lt;/strong&gt; part. The verification, or assertions of the values after the event happened.&lt;/p&gt;

&lt;p&gt;Putting this structure into a test, which tests the functionality to buy a book, could look something like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// given
String shouldBuyer = "hanswurst@test.com";
Long anyBookId = 1L;

// when
UserBook actual = sut.buyBook(shouldBuyer, anyBookId);

// then
assertThat("the actual owner of the book, should be the buyer", 
    actual.getBuyer(), equalTo(shouldBuyer));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Name of the Testmethod
&lt;/h2&gt;

&lt;p&gt;Normally I prefer short self-documenting names for methods. &lt;br&gt;
For tests though, I cut the "short" from this preference.&lt;/p&gt;

&lt;p&gt;A test should always say what it is trying to test. Of course, you could do this by documenting it with some Javadoc, or you could just name the test the way you would normally document it.&lt;/p&gt;

&lt;p&gt;Applying the &lt;em&gt;Given-When-Then pattern&lt;/em&gt; to this definition, the name for our book-buying test could look something like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Test
public void givenValidUserNameAndBookId_whenBuyBook_thenUserShouldOwnNewBook() {
    ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you ever googled the topic of long method names, you probably know that there are a lot of discussions on this topic, and they are for a good reason.&lt;/p&gt;

&lt;p&gt;Long names can imply a complex implementation for a method. &lt;br&gt;
And if you don't think you can shorten the name of the method, without losing information that the developer might require to understand the method, it's probably true that it's too complex.&lt;/p&gt;

&lt;p&gt;For tests on the other hand, this argument doesn't apply. We are just testing an implementation, and not implementing some logic. The tests are also not called from anywhere else but the test runner, so we are not cluttering our code with long method calls.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;The &lt;em&gt;Given-When-Then pattern&lt;/em&gt; can be used to define any given test scenario.&lt;br&gt;
Implementing it in the world of Software-Development can be done by following conventions agreed upon by the team.&lt;br&gt;
That is true for other patterns too.&lt;/p&gt;

&lt;p&gt;So I would like to conclude by saying whatever Pattern, Libraries or whatever Framework your team chooses to use, &lt;br&gt;
the most important thing is that your team agrees on what they use, so you can ensure a consistent way of developing and testing your software.&lt;br&gt;
This will help the team in the long run to further develop features, maintain the application and keep understanding what in the world is happening here.&lt;/p&gt;

&lt;h1&gt;
  
  
  Read more
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://martinfowler.com/bliki/GivenWhenThen.html"&gt;https://martinfowler.com/bliki/GivenWhenThen.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dannorth.net/introducing-bdd/"&gt;https://dannorth.net/introducing-bdd/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Behavior-driven_development"&gt;https://en.wikipedia.org/wiki/Behavior-driven_development&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>givenwhenthen</category>
      <category>java</category>
      <category>bdd</category>
      <category>testing</category>
    </item>
    <item>
      <title>Layer vs Feature Architecture</title>
      <dc:creator>smotastic</dc:creator>
      <pubDate>Thu, 27 Aug 2020 17:50:01 +0000</pubDate>
      <link>https://dev.to/smotastic/layer-vs-feature-architecture-3cko</link>
      <guid>https://dev.to/smotastic/layer-vs-feature-architecture-3cko</guid>
      <description>&lt;p&gt;I recently joined a new Project, and as always one of my first tasks was to become familiar with the existing codebase.&lt;/p&gt;

&lt;p&gt;In this JEE-Project, one of the most important things for me was to learn how the classes were distributed into different packages. Having this knowledge would mean that, even if I didn't know how something worked, I could easily know where to look and then learn how the code works.&lt;/p&gt;

&lt;p&gt;In this blog post, I want to show two of the most commonly known architectural design patterns on how one could organize the code.&lt;/p&gt;

&lt;h1&gt;
  
  
  Layer Architecture
&lt;/h1&gt;

&lt;p&gt;Or the &lt;strong&gt;horizontal&lt;/strong&gt; approach.&lt;/p&gt;

&lt;p&gt;Imagine a cake with multiple layers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdx55mhzrr7p4c0215bp4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fdx55mhzrr7p4c0215bp4.jpg" alt="layer cake"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each layer represents a package.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Each package contains exactly what this layer is responsible to do.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So imagine you organize your code into 4 Layers, let's say:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;UI&lt;/li&gt;
&lt;li&gt;Businesslogic&lt;/li&gt;
&lt;li&gt;DAO&lt;/li&gt;
&lt;li&gt;Model &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;(this of course is not the most optimal way of organizing your layers and is only used for easy representation purposes)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We would have 4 layers, or 4 packages. &lt;/p&gt;

&lt;p&gt;The UI is responsible for handling everything UI related. So this is where every UI class related to any feature would be created.&lt;/p&gt;

&lt;p&gt;The same applies to the other three layers. Businesslogic is responsible for everything related to business-rules, DAO handles the Database-Access, Model contains for example the Object Relational Model for the underlying database. &lt;/p&gt;

&lt;p&gt;If we would want to build an application that can create some tasks, and then list them, we could divide them accordingly into the 4 packages.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;UI

&lt;ul&gt;
&lt;li&gt;TaskListController.java&lt;/li&gt;
&lt;li&gt;TaskCreateController.java&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Businesslogic

&lt;ul&gt;
&lt;li&gt;TaskListBusinessrule.java&lt;/li&gt;
&lt;li&gt;TaskCreateBusinessrule.java&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;DAO

&lt;ul&gt;
&lt;li&gt;TaskListDao.java&lt;/li&gt;
&lt;li&gt;TaskCreateDao.java&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Model

&lt;ul&gt;
&lt;li&gt;TaskListModel.java&lt;/li&gt;
&lt;li&gt;TaskCreateModel.java&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Advantages
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Adding new classes in such an environment is really easy.&lt;/strong&gt;&lt;br&gt;
For example, if someone would want to create a new Model-Class, he could simply do it by adding it to the Model package without thinking twice.&lt;/p&gt;

&lt;p&gt;This also means that new team members will have an easy time learning on how to organize the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Disadvantages
&lt;/h2&gt;

&lt;p&gt;A big disadvantage for this approach is, that it is really &lt;strong&gt;hard to assess the complexity&lt;/strong&gt; of a feature, because you don't know where a feature is or might be used, and what dependencies it might have to other features.&lt;/p&gt;

&lt;p&gt;Also, think about encapsulation. &lt;strong&gt;Everything must be public&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The Businessrule probably has to access the DAO in some way.&lt;br&gt;
But why can the UI also access the DAO? Because either only other DAO's can access my DAO, or everyone can.&lt;/p&gt;

&lt;p&gt;Of course, this Argument only applies if you are still using Java8 like myself.&lt;br&gt;
Else you could use Jigsaw (Java9 Module-System) to create modules.&lt;/p&gt;

&lt;h1&gt;
  
  
  Feature Architecture
&lt;/h1&gt;

&lt;p&gt;Or the &lt;strong&gt;vertical&lt;/strong&gt; approach.&lt;/p&gt;

&lt;p&gt;Imagine a cake, with multiple slices.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fro67hk5biabea3m6614h.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fro67hk5biabea3m6614h.jpg" alt="sliced cake"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each slice represents a package.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Each package contains exactly what this feature is responsible to do.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So imagine our App-Example from before, if you would organize your code into a feature-like package structure, it could look like this.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TaskList

&lt;ul&gt;
&lt;li&gt;TaskListController.java&lt;/li&gt;
&lt;li&gt;TaskListDao.java&lt;/li&gt;
&lt;li&gt;TaskListBusinessrule.java&lt;/li&gt;
&lt;li&gt;TaskListModel.java&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;TaskCreate

&lt;ul&gt;
&lt;li&gt;TaskCreateController.java&lt;/li&gt;
&lt;li&gt;TaskCreateDao.java&lt;/li&gt;
&lt;li&gt;TaskCreateBusinessrule.java&lt;/li&gt;
&lt;li&gt;TaskCreateModel.java&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Advantages
&lt;/h2&gt;

&lt;p&gt;In contrast to the Layer Architecture, you can actually use encapsulation in such a structure, as &lt;strong&gt;every Feature is encapsulated in his own package&lt;/strong&gt;. Only the API's that really have to be public, can be public.&lt;/p&gt;

&lt;p&gt;In modern architectures, it's very common to &lt;strong&gt;create micro services&lt;/strong&gt;. With this structure already in place, it's really easy to extract existing features, into a micro service architecture.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Extandibility&lt;/strong&gt; (if this is a word, it suits here):&lt;br&gt;
If a feature needs to be extended by some functionality, you immediately know where you have to go, and in what context you will work.&lt;br&gt;
You can also be more certain, that with changes to some features, you will probably not break the whole application by doing so, only the feature that you are working on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Disadvantages
&lt;/h2&gt;

&lt;p&gt;Big Applications with lots of features, will quickly grow in not only their amount of classes, but also their &lt;strong&gt;amount of packages&lt;/strong&gt;. On the one hand, it might be hard to keep track of all the features, but on the other hand it might also be easier to identify certain features, if the packages have more meaningful names than just their layer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You actually have to know what you are doing&lt;/strong&gt;. If this is a disadvantage or not, you should decide for yourself. But also think about new people joining the team. Instead of immediately having the right place for each class, they actually have to know what they are currently working on, to precisely choose the correct package for their new class.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;So what should you choose for your Project? &lt;br&gt;
As always it depends.&lt;/p&gt;

&lt;p&gt;In general, and based on the amount of advantages I've listed for one of the patterns, I prefer the &lt;em&gt;Feature Architecture&lt;/em&gt;. It gives you more stability in the way you are working. If you change a class, you don't actually have to do a regression test on the whole application (at least in theory).&lt;br&gt;
But the &lt;em&gt;Layer Architecture&lt;/em&gt; also has it's strengths. I imagine that in smaller projects, this kind of architecture would actually be beneficial, because you can get a faster overview of all your classes.&lt;/p&gt;

&lt;p&gt;I would like to conclude by saying that neither of these solutions is the correct answer to all your problems. Each team should find out what kind of architecture fits for their requirements, or for their project.&lt;br&gt;
In a lot of cases I've seen a combination of the two.&lt;br&gt;
You could organize your classes into Feature-Packages (Feature Architecture), and in these packages you divide your classes into the layers (Layer Architecture). &lt;br&gt;
Only the team can decide, what is good for it.&lt;/p&gt;

</description>
      <category>java</category>
      <category>architecture</category>
      <category>designpatterns</category>
      <category>packagestructure</category>
    </item>
  </channel>
</rss>
