<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ievgen Major on Medium]]></title>
        <description><![CDATA[Stories by Ievgen Major on Medium]]></description>
        <link>https://medium.com/@ievgen-major?source=rss-48a11fea15b6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*TJkj12O0slvvsUgc.</url>
            <title>Stories by Ievgen Major on Medium</title>
            <link>https://medium.com/@ievgen-major?source=rss-48a11fea15b6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 21:27:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ievgen-major/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Spring and Strategy Pattern]]></title>
            <link>https://ievgen-major.medium.com/spring-and-strategy-pattern-36106fb4f04f?source=rss-48a11fea15b6------2</link>
            <guid isPermaLink="false">https://medium.com/p/36106fb4f04f</guid>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[junit-5]]></category>
            <category><![CDATA[oop]]></category>
            <category><![CDATA[oops-concepts]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Ievgen Major]]></dc:creator>
            <pubDate>Wed, 30 Dec 2020 17:17:34 GMT</pubDate>
            <atom:updated>2020-12-31T08:19:36.292Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*Q7JNrTwAQ7mnOyfrLKq6Nw.png" /></figure><p>As we all know from <strong>1994 </strong>old <a href="https://en.wikipedia.org/wiki/Strategy_pattern">good Strategy pattern</a> principle did not change, languages and frameworks did. So I will not talk about “how to implement yet another 100500 Strategy” and why it is a useful design pattern, but rather how to use specific Spring (or other IoC container) features to make it easier and efficient.</p><p>First of all, the strategy pattern is about how to organize code in a way, the exact algorithm could be selected in runtime and easily added or changed.</p><p>Implementation will require:</p><ol><li>Interface <em>IStartegy </em>which will be inherited by exact strategy <em>Strategy1 </em>and <em>Sattegy2 </em>etc.</li><li>Strategy selector witch will define, what strategy should be used for the particular condition (could be external or internal).</li><li>Processor class, that will collect all implementations and will delegate the call to exact strategy.</li></ol><h3>Approach1 switch-case</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d9c79313fef40ff6c940e61eec5f06e7/href">https://medium.com/media/d9c79313fef40ff6c940e61eec5f06e7/href</a></iframe><p>“switch — case” is used to define what strategy to call for request. I’ve used private methods to simplify code, implementation could be inner classes, lambdas of spring beans. The main problem of this approach is the “switch” statement. Also testing for this type of implementation will grow if additional items will be added.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/84ac54297d792a74cd7392bec8f83e30/href">https://medium.com/media/84ac54297d792a74cd7392bec8f83e30/href</a></iframe><p>Every time you need to implement a new strategy:</p><ol><li>add an item to enum</li><li>write implementation</li><li>add one more “case” statement</li><li>write test</li></ol><p>Also we can see, there is no way to add a new strategy without CaseStrategyImpl code modification.</p><h3>Approach2 IoC way</h3><p>Lets use Spring @Component for strategy implementation. Spring Component naming and Spring @Autowiring for strategy registration in loan processors container.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0376f8ac23183ebb5bc0044c67388154/href">https://medium.com/media/0376f8ac23183ebb5bc0044c67388154/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/60067bf1454747b63185ae0e6ab4f658/href">https://medium.com/media/60067bf1454747b63185ae0e6ab4f658/href</a></iframe><p>This approach removes <strong>the switch </strong>and allows you to write only strategy implementation, and spring (or other IoC container) will do all registration work for you.</p><p>From testing point of view, You will write test for exact strategy implementation</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/90cdb5424fb53540381c860d7a529d5c/href">https://medium.com/media/90cdb5424fb53540381c860d7a529d5c/href</a></iframe><p>And only 1 test for processor</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ecb3907c75d556df072bbf6737a8da51/href">https://medium.com/media/ecb3907c75d556df072bbf6737a8da51/href</a></iframe><p>What is the main disadvantage of this approach? Well, the main problem here is that our enum <em>LoanType </em>is only semantically connected with the component name, so refactoring and correct component naming could be error-prone. <em>Also, try not to use class names as strategy identifiers. You will expose internal implementation details and will not be able even to rename the class without external modifications</em>.</p><p>So now every time you need to implement a new strategy:</p><ol><li>add an item to enum</li><li>write implementation and name it accordingly</li><li>write test for strategy, test for processor not needed</li></ol><p>Well, we removed one implementation step and delegated it to Spring. The number of test cases also reduced. We have saved time and money. Can we do better?</p><h3>Approach3. Compile time validation</h3><p>Let’s introduce the interface method <em>LoanType getCode()</em> and implement it in every strategy.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/19a25a57daa32b872066539693291802/href">https://medium.com/media/19a25a57daa32b872066539693291802/href</a></iframe><p>Processor will use <em>evaluate method</em> and every strategy inherit it from default implementation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/55ada18224fefeb2f26240e6df6d5f7c/href">https://medium.com/media/55ada18224fefeb2f26240e6df6d5f7c/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2dfda0c0794185bba70653ac89da5bdd/href">https://medium.com/media/2dfda0c0794185bba70653ac89da5bdd/href</a></iframe><p>As You can see processor also slightly changed. We collect strategies to EnumMap using <em>LoanType</em> as a key. Starting from now compiler will check everything for us. We are always writing more code to write less code)</p><p>What about tests? In my opinion whey testing exactly what they should.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1724bab5f29b96f8a406c919977d2b4/href">https://medium.com/media/e1724bab5f29b96f8a406c919977d2b4/href</a></iframe><p>Can we do better?</p><h3>Approach4. Compile time validation with Generics</h3><p>Well we can assume we have big application, and using strategy a lot. So it will be a good idea to have 1 abstract implementation to use everywhere. Reuse always has benefits and some generics may help, lets see.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ab4b935b4a7717074977f1fc18c3bea5/href">https://medium.com/media/ab4b935b4a7717074977f1fc18c3bea5/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3f2ab69a2006265033c7f39f919ce5e7/href">https://medium.com/media/3f2ab69a2006265033c7f39f919ce5e7/href</a></iframe><p>We defined generic strategy interface, and abstract implementation for processor. Now all we need is to inherit them in our application. All registration and handling process will be provided by our strategy framework.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/094611f9a405de3f7602253ef14c28fd/href">https://medium.com/media/094611f9a405de3f7602253ef14c28fd/href</a></iframe><h3>One small bonus</h3><p>There are one more approach to register strategy.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/35625636a91f6e62903d970205b001fb/href">https://medium.com/media/35625636a91f6e62903d970205b001fb/href</a></iframe><p>The trick here is combination of interface and <em>@Autowired</em> annotation, spring will invoke method register and register strategy in registry, and then</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b41f9b8e1b05ea6f5c79edf4e3888702/href">https://medium.com/media/b41f9b8e1b05ea6f5c79edf4e3888702/href</a></iframe><p>You will be able to puzzle you colleges with one more nice strategy implementation)</p><p>All codes and tests available in <a href="https://github.com/major13ua/spring-strategy-pattern">repository</a>.</p><p>Happy coding and a happy new year!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=36106fb4f04f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Micronaut integration testing using testcontainers]]></title>
            <link>https://ievgen-major.medium.com/micronaut-integration-testing-using-testcontainers-d7eccc50eda6?source=rss-48a11fea15b6------2</link>
            <guid isPermaLink="false">https://medium.com/p/d7eccc50eda6</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[testcontainer]]></category>
            <category><![CDATA[junit-5]]></category>
            <category><![CDATA[micronaut]]></category>
            <dc:creator><![CDATA[Ievgen Major]]></dc:creator>
            <pubDate>Wed, 14 Oct 2020 20:25:08 GMT</pubDate>
            <atom:updated>2020-10-14T20:25:08.533Z</atom:updated>
            <content:encoded><![CDATA[<p>Micronaut is a modern, JVM-based, full-stack framework for building modular, easily testable microservice and serverless applications. Concepts, API, annotations, and conventions are similar to Spring Framework, but some features should be done differently or missing.</p><p>One of these missing is ApplicationContextInitializer which is really handy for application context configuration in runtime, before component initialization. This feature is used to get Testcontainers instance host and port, as Testcontainers uses randomized ports for each container it starts.</p><p>Spring Boot configuration and ApplicationContextInitializer described with great details in the <a href="https://dev.to/sivalabs/springboot-integration-testing-using-testcontainers-starter-13h2">article</a>, and here I will try to fill the gap for Micronaut.</p><h3>Option1. Lazy and deprecated.</h3><p>withCreateContainerCmdModifier could be used to set known port instead of random.</p><pre>public abstract class ResourceProvider {<br>  private static final int REDIS_PORT = 6379;</pre><pre>  @Container<br>  static GenericContainer redis = new GenericContainer&lt;&gt;(“redis:5.0.8-alpine”).withCreateContainerCmdModifier(<br>    it -&gt; it<br>      .withName(“redis-unit”)<br>      .withPortBindings(<br>        new PortBinding(Ports.Binding.bindPort(REDIS_PORT), <br>        new ExposedPort(REDIS_PORT))))<br>      .withExposedPorts(REDIS_PORT);<br>}</pre><p>but, withPortBindings deprecated, and will be removed in future releases. Main idea behind this, is virtualization and scoping. Your application tests should be independent, so they could be executed in parallel, using same docker. Also Docker/Jenkins environment is frequently used for multiple projects, and in this case you will receive “Bind for 0.0.0.0:6379 failed: port is already allocated” when both tests will require same container.</p><h3>Option2. Micronaut way solution.</h3><p>Micronaut offers PropertySource concept to initiate application configuration with host/port values obtained from testcontainer. <br>Example for EmbeddedServer</p><pre>@BeforeAll<br>public static void initUnitTest() {<br><br>    <em>embeddedServer </em>= ApplicationContext.<em>run</em>(EmbeddedServer.class, PropertySource.<em>of</em>(<br>            &quot;test&quot;, Map.<em>of</em>(&quot;redis.host&quot;, <em>redis</em>.getContainerIpAddress(), &quot;redis.port&quot;, <em>redis</em>.getMappedPort(<em>REDIS_PORT</em>))<br>    ));<br><br>    <em>context </em>= <em>embeddedServer</em>.getApplicationContext();<br><br>}</pre><p>Example for ApplicationContext</p><pre>@BeforeAll<br>public static void initUnitTest() {<br>    <em>context </em>= ApplicationContext.<em>run</em>(PropertySource.<em>of</em>(<br>            &quot;test&quot;, Map.<em>of</em>(&quot;redis.host&quot;, <em>redis</em>.getContainerIpAddress(), &quot;redis.port&quot;, <em>redis</em>.getMappedPort(<em>REDIS_PORT</em>))<br>    ));<br>}</pre><p>End to end demo could be found <a href="https://github.com/major13ua/micronaut-tc">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d7eccc50eda6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>