Tag Archives: TDD

Test Driven Development with SpringBoot+Junit

 

Test-driven development(TDD) in the Spring framework.

TDD  helps me speed up my software release cycles, and ensure that I end up with a high-quality product. Spring makes Java enterprise development much easy.  Spring Boot, which we will use in this blog, is a project that does this task with excellence.

Follows are the steps to configure your spring boot framework with the Junit and Maven.

Step-1)Set up Spring boot with J-Unit testing Approach

After Set your maven dependencies , for spring-boot add following dependency for testing Approach.

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
<scope>test</scope>
</dependency>

Step-2)Create test Package and in that ,create DemoApplicationTests Class.

Step-3)Inject controller using @InjectMocks, Inject repository using @Mock Annotations.

Let us take some J-Unit Annotations used in java class :

@Before:@Before annotation are executed before each test.

@After:It executes after each test.

@BeforeClass: It executes common operation before each test,So it executes  only once before running all tests.

@AfterClass: It executes common operation after each test, So it executes only once after running all tests.

@Test:The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case.

In JUnit 5, the tags @BeforeEach and @BeforeAll are the equivalents of @Before and @BeforeClass in JUnit 4.

Configure Java File and Select Run as Junit and Test the file.

 

Congratulations!!!you are done with your TDD approach.

Creating a new git project on xp-dev.com with IntelliJ!

Meta Steps, YMMV…

1/Select what you need from Spring INITIALIZR :: https://start.spring.io/
2/Download/Extract the zip && cd into it.
3/Create a git repo on xp-dev.com
4/Initialize Git repo:
a)git init
b)git add .
c)create .gitignore
d)git commit .
e)git remote add origin https://xp-dev.com/git/
f)git push -u origin master
5/Open IntelliJ
a) checkout https://xp-dev.com/git/
b) Goto Maven Projects and add the pom.xml(if not picked automagically by IntelliJ
c)Do deploy, I got a failing unit test, as I added spring-tests.

My JUnit Arsenal!

Writing unit tests for working on a piece of code is an exciting task that I enjoy.
To accomplish this with ease, for now my junit arsenal has the following approach:

1/ Mathematical Induction

2/ Boundary Value Analysis

3/ Permutation of the variables.

What’s yours?

Care to share?

To Model or Not to Model is the Question!

For Model is optional/and possibly an impediment, unless asked for.

The following picture depicts my understanding of what piece of technology resides where when writing the code from a UI->Business->DB.

When doing TDD<->Code, there are very short and quick feedback cycles due to which there is good paced development.

When doing it with modeling involved as the short feedback cycles turns into a painstakingly long , and hence looses the pace that could have been achieved with no MDA at all.
Even with MDA as a choice, is it beneficial to extract technical details from the model or derive the model from the interface?

The interfaces act as the point of inflection of choosing whether to MDA or not.

I am not very much into Big-Upfront-Design camp,maybe due to lack of experience but as I understand every software solution in the writing has its own characteristics that are better dealt with when dealt with no big designs that over-shadow its actual requirement.

With features like reverse engineering from code-to-UML, although the cycle could be shortened, but only to load the design with something conceptual at a high level, even with a high probability that the same understanding could be useless or simply a cause of not being able to implement an elegant solution.

The fact that MDA is dependent on the interfaces to proceed and TDD creates those interfaces, not by thinking-out the complete system, but on a case-by-case basis, so the two approaches can exist, but in true agile spirit?

I don’t think so.

~rohit.