Inflearn brand logo image
Inflearn brand logo image
Inflearn brand logo image
BEST
Programming

/

Back-end

A developer's cheat sheet for adding Java/Spring tests

Learn how to add tests to Spring! Then, learn how to change the Spring design to make testing more natural.

(4.9) 167 reviews

2,218 learners

  • kok202
tdd
테스트
백엔드
TDD
Software Test
unittest
Spring
JPA

Reviews from Early Learners

What you will learn!

  • How to put tests in Spring

  • Concepts needed for testing

  • How to do a natural test

  • How to test without Mockito

  • Dependency and Testability

  • Hexagonal Architecture and Design

Developing project design
Let me explain the essence of the test!

Starting with how to add tests to Spring, and ending with an architecture lecture!

Because I thought that in order to give you the right opinion about testing, it ultimately has to end with architecture.

Learn the basic concepts of testing and how to integrate tests into Spring. Furthermore, learn how to change the Spring design to enable natural testing.

So, this is what we learn.

The test
Why is it needed?

About the test
Basic Concepts

In spring
Putting the test
method

Spring design
Evolving
method

Clean
Architecture

Hexagonal
Architecture


But, have you ever thought this?
Concerns and misunderstandings about testing

“I really want to try the test. But...”

My friends at other companies are steadily adding tests to keep up with the demands of the times and are growing step by step. But our company doesn't have tests. I don't want to fall behind. I have the will to study. But I just can't figure out how to add test code to this legacy system .

“I am getting excited about testing with H2.”

Testing with H2 is too slow and too dependent on the DB. All tests use H2. It is burdensome to run tests, and since tests share h2 , data gets tangled, so sometimes tests pass and sometimes they fail .

“I don’t feel the need for testing.”

Our team is not that many people, and I have never felt that anxious while deploying. The requirements change all the time. But how do you do TDD? I think testing only slows down the development speed .


Test, I had the same concerns.

If you look at the books or seminars on testing on the market, most of them just talk about the advantages of testing, how to write test code, JUnit, Mockito, and so on, and end there. The examples are simple enough to suit the taste of beginners. So when I close the book at the end, I think this.

  • "How on earth do you use this in the field?"
  • "How do I add tests to Spring?"
  • "This is a test that is put in when there is no framework, so isn't it easy to put it in?"

It was confusing. JPA test using H2... well, it's good. But how can I test it if I don't have a test DB like ElasticSearch or Cassandra? In addition, the test using H2 was really slow to the point of being fatal. It was burdensome to run it several times a day. The test broke frequently, and on days when the DB table changed, I had to modify all the related tests, so it often felt like a waste of time.

If you want to study TDD, you shouldn't start with TDD.

Then, all my worries were solved when I started studying architecture and DDD . I know. TDD is always hot. But TDD is a really attractive but difficult methodology. If an organization that has never done TDD tries to develop with TDD because TDD is popular, will it be okay? TDD really requires a lot of prior knowledge and empathy before starting, more than you think.

So, if you want to study TDD, you shouldn't start with TDD. You should start with testing. Just like you need to know how to handle equations before you can do calculus.

Let me tell you my story about testing!


Concerns about testing
I'll solve it clearly for you.

If you don't feel the need for testing

If you don't feel the need for testing, then your project is not big enough yet, or you have a good enough development capability and culture to not need testing. Testing is not the answer! Many great projects were developed even in the days when there were no tests. Testing is a tool for getting good software, not a goal in itself.

If you don't know how to get good design through testing,

It seems that you studied too much only on how to do tests . It is certainly meaningful to study how to use JUnit, how to use Mockito, and how to link h2, but that is not the essence of testing. You need to learn why you should do tests, what the goals are, and how to practice to achieve those goals.

If you don't know how to test without a test framework,

The question about test frameworks is also the same answer as the previous question. If you are in an environment where you use Typescript + Nest.js , how will you test it? You need to learn how to test it without a test framework like Mockito or H2 .


A must-have for Spring developers
You need to learn how to give tests .

  • ✅ We will not teach you how to create tests that are unrelated to actual practice.
  • ✅ We will teach you how to add tests required by Spring developers.
  • ✅ At the same time, we will tell you why you should do it this way .

The knowledge based on this principle is knowledge that can be applied to other frameworks as well. Whether you use Nest.js, Swing UI, or Gin... I think it is knowledge that can be applied anywhere.

The final chapter will be devoted to a lecture on design . This is because I felt that design should not be left out in order to fully explain the value of testing.

At least, I haven't found any books or lectures that teach you how to add tests to Spring in Korea. Everyone uses annotations like @Mock and @Spy to mock and inject dependencies to write tests. But what if there is no such Mock library in an environment? Essentially, you need to know how to test without these annotations ! And you need to be able to distinguish what you need to test.


Adding tests as tests
I'll show you how!

You will learn the need for testing .

Learn how to add tests to Spring without a framework .

You will discover a better way to use Spring .

You will understand how to transition from a layered architecture to a hexagonal architecture .

Orientation

Let's look at why TDD fails when it is introduced in an organization that wants to add tests. Let's look at why TDD, which I usually do, fails. Let's look at the problems that occur when you start adding tests without a proper understanding of tests.

Theory of Testing

Study basic test knowledge. Empathize with the need for testing and understand the correlation between testing and design. Understand what developers should consider when creating tests. Finally, we will look at the work of increasing testability for natural testing.

Part 1 - Applying Testing Based on Toy Project

Let's take a look at the toy project that we will use in the future. Then, we will randomly add tests to the toy project and achieve 100% coverage by adding as many tests as possible. In this process, we will use external libraries such as h2, MockMvc, and PowerMock.

Let's take a look at the final tests that were created and see what's wrong with tests that only cover regression bugs.

Directional exploration

We will look at why putting tests in our toy projects is not a good idea. We will also look at the problems with traditional layered architecture. And we will hear the speaker's opinion on how to improve it.

If you struggled to just add tests in Part 1, in Part 2, you will learn how to mock naturally without an external library like Mockito. Let's take a look at how to change the code in advance for Part 2.

Part 2 - Toy Project Structure Design Improvement

We apply the insights gained through the previous direction exploration to our toy project. In this process, we improve the design by making structural changes. And in this process, the following techniques are used.

  • Improved package structure
  • Distinguishing between domain and persistent objects
  • Bringing service logic into the domain
  • Inverting dependencies for external linkages
  • Make tests into small tests

Evolving Architecture

We will explore how to evolve architecture through the correlation between testing and design. We will see how layered architectures evolve into hexagonal architectures. We will learn how to focus developers on the domain by separating concerns.


Growing as a developer
I'd like to help!

Hello, I'm Woogeun Kim ! 👋

I am currently working at Kakao, and I love making things, so I am always developing something after work. There is a saying, "A dwarf standing on the shoulders of a giant." I am just a small dwarf, but I am trying to pass on my knowledge so that I can help the giants grow. I have experience mentoring many junior developers, so I think I can help you grow.


Q&A 💬

Q. Can you tell me how to use JUnit or Mockito?

Yes, unfortunately, this course does not cover how to use JUnit and Mockito. Here are the reasons why:

Context) Each student is in a different situation. If someone has already written a few tests, their proficiency in handling libraries may vary. The library used by the company may not be JUnit or Mockito. There may be interfaces that are not provided because the version is different. For example, JUnit4 does not support @ParameterizedTest. In addition, the usage of the libraries mentioned above is not that difficult, and there are already too many good related materials. I think that you can learn how to use them sufficiently just by reading the Github README file or the official guide. Therefore, I thought that the usage was not important.

Test Design) Learning how to test using a specific function provided by a specific library means that you cannot do that test without that library. How to design tests and how to turn them into testable structures is more important than tools or frameworks. Therefore, this lecture does not focus on tools. Testing is a part of the development process. Therefore, I thought it was more important to check the points that should be considered in the context of requirements analysis, design and deployment, and maintenance.

Q. What are the differences from previous lectures ?

As you can see from the curriculum, there is some overlap with previous lectures. It is not convincing enough to explain in detail what should be done, and it is not possible to understand the context, so the lectures are organized so that you can take the relevant lectures independently. So please be sure to check the table of contents and see if there is any overlap.

To be honest, if you have properly understood the previous lectures, I don't think you need to take this lecture again. If the previous lectures were lectures that explained why the structure should be like this, you can think of this lecture as a lecture that actually deals with how to solve it at the code level.

Q. Is there anything I need to know before taking the course?

This lecture is not a lecture that teaches Spring or JPA. It is assumed that the student can handle Spring and JPA in general. In addition, it teaches how to add tests to Spring , but it focuses on how to resolve dependencies , and I don't think it is necessarily limited to a language or framework. In addition, specialized knowledge is not required :)

💾 Please check before taking the class!

  • The practical content in the lecture is designed to be as independent as possible from specific libraries, IDEs, or specifications. We have tried not to make lectures that utilize specific features in specific versions. However, just in case, we would like to inform you that the lecturer's laptop configuration is as follows.
    • PC specs and operating system: MacBook Pro (16-inch, 2019) and macOS Monterey 12.6
    • IDE: IntelliJ IDEA 2021.3.2
    • Java 17, Spring Boot 3.0.1, JUnit 4.13.2, AssertJ 3.23.1
  • We provide students with 12 PPT materials, each approximately 40 pages long.
  • This course does not cover programming languages or how to handle Spring.
  • This explanation is based on the understanding of users who have worked with Spring and Spring Data JPA at least once.
  • I will explain assuming you know about Controller, Service, and Repository.
  • I have provided a preview lesson of the toy project that will be used in the lecture. Please check it out and if you can understand it, it is fine.
  • It is permitted to capture a portion of the PPT and post the studied content on a blog, but sharing the entire material on a personal blog is prohibited. Thank you for your understanding!
  • There is some overlap with previous lectures. Please check the table of contents to see if there is any overlap.
  • Please see the demo project here .

Recommended for
these people

Who is this course right for?

  • If you want to add tests to Spring

  • If you are confused about how to add tests to Spring

  • If you are confused about whether the Spring project you developed has the correct design structure

  • Those who tried to insert the test but failed

  • Those who want to put tests in NoSQL that doesn't have a test DB like H2

Need to know before starting?

  • You can understand Java code

  • Spring Basics (For those who know what Controller/Service/Repository are)

  • Have you used JPA?

Hello
This is

3,405

Learners

241

Reviews

47

Answers

4.9

Rating

2

Courses

  • (현) 카카오 백엔드 엔지니어
  • (수상) 🏆 공개 SW 개발자 대회 [2020 일반부문 / 금상_정보통신산업진흥원장상] 

 

현재 카카오에서 일하고 있고, 만드는 것을 좋아해서, 퇴근 후에도 항상 무언가를 개발하고 있습니다.

"거인의 어깨 위에 선 난쟁이"라는 말이 있습니다. 저 역시 한낱 작은 난쟁이일 뿐이지만, 올라탄 거인의 성장에 도움이 될 수 있도록 지식의 대물림을 위해 노력하고 있습니다. 다수의 주니어 개발자분들을 멘토링 한 경험이 있어서 여러분의 성장을 도와줄 수 있을 거예요. 

 

깃허브 > https://github.com/kok202
블로그 > https://kok202.tistory.com

Curriculum

All

25 lectures ∙ (6hr 20min)

Course Materials:

Lecture resources
Published: 
Last updated: 

Reviews

All

167 reviews

4.9

167 reviews

  • f2idoijsdo님의 프로필 이미지
    f2idoijsdo

    Reviews 2

    Average Rating 3.0

    5

    100% enrolled

    I can confidently say that this is the best lecture among all the lectures I paid for at Inflearn. The original curriculum structure that was created by racking my brain without copying from books, the essence of each lecture, no rambling, clean comments and sentences, appropriate speed and clear speech, and the concrete practical method of testing to draw the big picture of design, and then draw the test under the design again. You can see how much the instructor has thought, repeated, improved, and developed. He has amazingly incorporated all of that into the lecture. I have only taken one or two lectures at Inflearn, but I have never seen A/S as refined as this lecture. After filming, videos are not as easy to edit as text, and due to the nature of the lecture, there must be a strong desire to always show something beautiful and without a single mistake, so I have refined those parts without any additions or subtractions. When the instructor makes a mistake or says, "I wish I had done this," there is a section where the screen turns gray and only the voice appears. When this part comes up, I can check if I'm listening with my wits about me, compare it to my thoughts, and think that the instructor also has such concerns. Anyway, these A/S videos are like a well-written script, and they serve as great educational tools. I bought the test, but the design came with it. But the design was more expensive than the test. I'm writing a review for the first time because I'm mad that there are only 54 reviews for this crazy great lecture. The content, voice, audio, video, and length... everything was cohesively put together, so it was like looking at a beautiful design. Personally, I liked Instructor Kim Woo-geun's lecture much better than the named lecture with +9,999 students.

    • xzxz70032590

      Ohhhh...

  • dfghcvb11님의 프로필 이미지
    dfghcvb11

    Reviews 5

    Average Rating 5.0

    5

    100% enrolled

    This lecture is not a simple test lecture. I was learning and applying how to use JUnit, Mockito, etc. to write tests. Then, I became curious about good test code and well-written test code beyond simply using test tools, and I found this lecture. I had enjoyed watching the instructor's first lecture, so I had a high level of trust in it, and the contents of the table of contents seemed interesting. I took all the lectures and am leaving a review. This lecture is not a simple test lecture, but a very great lecture that helps me think about good architecture and OOP. For me, whose purpose of writing tests was simply to prevent regression, testing and design are mutually complementary, and it made me think about good design naturally while informing me of the problems of the existing layered architecture, and the limitations of tests written in the layered architecture. It does not just point out problems in theory, but suggests better structures and test code writing through the process of refactoring code. I was learning Mockito for the first time recently and was impressed by the tests using stubbing. This was a great experience for me to test the tests that required Mockito in pure Java without an external library. In the end, it is a good lecture that continues the words of the first part that everything is OOP. In addition, it was good to talk about the concepts needed for testing and the opinions that contained the instructor's personal concerns. I learned a lot because I could indirectly feel the seriousness and attitude toward the development field. Thank you for the great lecture.

    • saechimdaeki님의 프로필 이미지
      saechimdaeki

      Reviews 48

      Average Rating 5.0

      5

      100% enrolled

      It was so much fun to watch and I think I'll reflect on it when newbies ask us, "Don't we have test code?" ㅠ_ㅠ.

      • 33cks14231753님의 프로필 이미지
        33cks14231753

        Reviews 4

        Average Rating 5.0

        5

        100% enrolled

        While studying Spring and looking at the codes on how to write tests, I noticed that there were many meaningless, repetitive tests. So I wondered why they were writing such repetitive tests. Then, I happened to see a new lecture on Infraon titled Why Spring Test Developers Fail to Write Tests. I thought it was just for me! So I boldly paid for it. After completing the course, I think I made a choice I don't regret. I paused the lecture for 10 minutes and thought carefully about what the instructor was talking about. So, although the lecture was 6 hours long, I felt that it had the quality of 60 hours. I am grateful to the instructor for broadening my perspective on software engineering.

        • carrykim님의 프로필 이미지
          carrykim

          Reviews 4

          Average Rating 5.0

          5

          48% enrolled

          I've never left a review for a class before, but this course is awesome.

          $46.20

          kok202's other courses

          Check out other courses by the instructor!

          Similar courses

          Explore other courses in the same field!