Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2024-04-15 원하는 테스트를 정의하고 테스트코드 짜보기 #4

Open
jyeonjyan opened this issue Apr 15, 2024 · 9 comments
Assignees
Labels
question Further information is requested

Comments

@jyeonjyan
Copy link
Owner

jyeonjyan commented Apr 15, 2024

본인이 만든 테스트코드를 자랑해봅시다. 🤔

@Test
@DisplayName("1+1=2 이다.")
public void onePlusOneIsEqualsTwo(){
    assertThat(1+2).isEqualTo(4);
}
@jyeonjyan jyeonjyan added the question Further information is requested label Apr 15, 2024
@jyeonjyan jyeonjyan self-assigned this Apr 15, 2024
@kimgh06
Copy link

kimgh06 commented Apr 15, 2024

package com.example.demo;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import static org.assertj.core.api.Assertions.assertThat;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.Collections;
import java.util.List;

@SpringBootTest
class DemoApplicationTests {

	@Test
	@DisplayName("1+1")
	void contextLoads() {
		assertThat(1+2).isEqualTo(3);
	}

	@Test
	@DisplayName("list")
	void asdf(){
		List<String> list = List.of();
		assertThat(list).isEqualTo(Collections.emptyList());
	}

	@Test
	@DisplayName("length")
	void getLength(){
		String str = "asdf";
		assertThat(str.length()).isEqualTo(4);
	}

	@Test
	@DisplayName("get hello")
	void getHello(){
		String hell = "hell", o= "o";
		assertThat(hell+o).isEqualTo("hello");
	}

	@Test
	@DisplayName("is it string?")
	void isString(){
		Integer asdf = 4;
		assertThat(String.class.isInstance(asdf)).isFalse();
	}
}

@Umjiseung
Copy link

package com.example.demo;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public class TestClass {

    @Test
    @DisplayName("list에 null로 인덱스 0번에 123을 넣고 비교하면 기댓값과 똑같나?")
    public void awdwd() {
        List<String> list = null;
        list.set(0, "123");
        assertThat(list.get(0)).isEqualTo("123");
    }
}

@joengsejun
Copy link


import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class NewTest {
    @Test
    @DisplayName("hello world인가?")
    public void isHelloWorld() {
        String message = "hello world";
        assertEquals("hello world", message);
    }
}

@seonmooKang
Copy link

seonmooKang commented Apr 15, 2024

package com.example.demo;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.test.annotation.DirtiesContext;

public class TestPrac {
    @Test
    @DisplayName("ㅇㅇ을 본인 이름으로 변환")
    public void ReplaceWord(){
        String Word = "ㅇㅇ을 위한 주먹";
        String name = "진원";
        Word.replace("ㅇㅇ", name);
    }
}

@Ppjh1212
Copy link

Ppjh1212 commented Apr 15, 2024

package com.example.demo;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class Test {
    @Test
    @DisplayName("문자열 중 키워드의 index값을 찾는다.")
    public void indexOf() {
        String name = "광주 소프트웨어 마이스터고 AI과";
        int index = name.indexOf("AI");
        System.out.println(index);
    }
}

@gkjdsvg
Copy link

gkjdsvg commented Apr 15, 2024

package com.example.demo.class3;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ts {
        @Test
        public void testReplaceWord() {
            // Given
            String sentence = "이 문장은 치환될 것입니다.";
            String target = "치환";
            String replacement = "변경";

            // When
            String replacedSentence = sentence.replace(target, replacement);

            // Then
            assertEquals("이 문장은 변경될 것입니다.", replacedSentence);
        }
    }

@kdy071115
Copy link

	@DisplayName("name")
	public void name(){
		String name= "kadiey2403";
		assertThat("kadiey"+"2403").isEqualTo(name);
	}```

@tlsgmltjd
Copy link
Contributor

@Test
@DisplayName("coolMethod를 실행시키면 RuntimeException이 발생한다.")
public void coolMethodThrowException() {
    assertThrows(RuntimeException.class, this::coolMethod);
}

private void coolMethod() {
    throw new RuntimeException("exception");
}

@Test
@DisplayName("trim을 사용하여 문자열 앞 뒤 간격을 제거한 후 글자수를 비교한다.")
public void trimLength() {
    String name = "   신 희 성     ";
    String trimName = name.trim();

    assertThat(name.length()).isEqualTo(13);
    assertThat(trimName.length()).isEqualTo(5);
}

@11dlguswns
Copy link

11dlguswns commented Apr 22, 2024

    @Test
    @DisplayName("String을 사용한 불변 연산보다 StringBuffer을 사용한 가변 연산이 빠른 처리 속도가 나온다.")
    void test2() {
        String string = "";
        StringBuffer stringBuffer = new StringBuffer();
        long stringLogicTime = timeCheck((i) -> string.concat(i));
        long stringBufferLogicTime = timeCheck((i) -> stringBuffer.append(i));

        Assertions.assertTrue(stringLogicTime > stringBufferLogicTime);
    }

    private long timeCheck(Source source) {
        long StartTime = System.currentTimeMillis();
        for (int i = 1; i <= 100000; i++) {
            source.logic(String.valueOf(i));
        }
        long EndTime = System.currentTimeMillis();
        
        return EndTime - StartTime;
    }
        
    private interface Source {
        void logic(String i);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

10 participants